cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
KarlConstruct
Frequent Visitor

Calculating Average Time based on a Status

Hello,

 

I am struggling with a problem calculating average days in Power BI Desktop.

 

I have the following data in their own columns;

 

  • Status Column
  • status.jpg
  • Created date (date/hh/mm/ss)
  • Last Modifed date (date/hh/mm/ss)

created_last modified.jpg

 

My aim is to find a formula/s which will help me calculate;

 

  • The time interval (in days) spent between Date Created and Last Modified only if the status is set to "Passed". 

Does anyone know the steps, or formula's that i can use to calculate this information? 

 

Thanks!

1 ACCEPTED SOLUTION

So in PowerBI there are 2 locations to create custom calculated columns:

  1. Through Query Editor using PowerQuery (M) language
  2. Though the main window using DAX language. This is similiar to excel.

I gave you the DAX version because it's easier to read/understand/manipulate IMO. With DAX, you don't need to reload your data everytime there is logic changed on a caluclated column. Instead, DAX uses your existing loaded dataset, and processes the logic. This makes is much more agile than SQL or PowerQuery. However, I still write native SQL queries to pull my data, and even build calculated columns in SQL. I tend to avoid PowerQuery if possible, though it has it's benefits like parsing JSON.

 

Going Forward:

  1. Delete that step in PowerQuery
  2. Load your data
  3. From the Home Tab, Add a Calculated Column
  4. Paste my formula
  5. View the data on the Data view on the left side of the main Pbi window

Add FieldAdd Field

 

 

 

 

 

 

View solution in original post

4 REPLIES 4
jsh121988
Microsoft
Microsoft

This is fairly easy in DAX with a new calculated column.

 

PassedDays =
IF( [Status] = "Passed",
DATEDIFF([Created], [LastModified], SECOND) / 60 / 60 / 24,
BLANK()
)
// I intentionally do a datediff using seconds and divide because days rounds down and it's not an accurate representation.
// This means a DATEDIFF('2019-01-01 23:59','2019-01-02 00:01', DAY) = 1 Day even though it's 2 minutes.

Also, this MUST return BLANK() if not 'Passed' so the Non-Passed items don't get calculated in the average.

 

Thank you for the info, when I plugged in this formula to a custom column, I am recieving errors.  Could you give me any pointers on what im doing wrong here?  (see screenshots below)

 

Appreciate the help. 

 

Formula entered into custom columnFormula entered into custom column

error receivederror received

So in PowerBI there are 2 locations to create custom calculated columns:

  1. Through Query Editor using PowerQuery (M) language
  2. Though the main window using DAX language. This is similiar to excel.

I gave you the DAX version because it's easier to read/understand/manipulate IMO. With DAX, you don't need to reload your data everytime there is logic changed on a caluclated column. Instead, DAX uses your existing loaded dataset, and processes the logic. This makes is much more agile than SQL or PowerQuery. However, I still write native SQL queries to pull my data, and even build calculated columns in SQL. I tend to avoid PowerQuery if possible, though it has it's benefits like parsing JSON.

 

Going Forward:

  1. Delete that step in PowerQuery
  2. Load your data
  3. From the Home Tab, Add a Calculated Column
  4. Paste my formula
  5. View the data on the Data view on the left side of the main Pbi window

Add FieldAdd Field

 

 

 

 

 

 

Thanks for the explaination, this helps a lot! 

Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors