How to Calculate Average with Import-Csv Excluding Zeros?

0
5
Asked By SunnyDaze42 On

I'm working on a script using Import-Csv to calculate the average of a specific column from an Excel file. However, I'm running into a problem where zeros are being factored into the average calculation, even though I've set up a conditional to filter out numbers that are greater than zero. I'm sharing my script in the comments because the main part of my post keeps getting auto-deleted for some reason.

1 Answer

Answered By TechWhiz101 On

It sounds like you're overcomplicating things a bit! You can actually calculate the average while excluding zeros by using Measure-Object. Just do this: `Import-Csv $file | Where-Object Score -gt 0 | Measure-Object -Property Score -Average`. The average will be in the output, and it should work perfectly for you.

SunnyDaze42 -

Thanks! Looks like that fixed it!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.