Calculating Power Factor

Please check the @KSKelvin post, it might be suitable for your application.