Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
bdeleur
Helper II
Helper II

Assistance Needed: Calculating Average Duration per Month in Database with DAX Code

Good afternoon,

 

I'm looking for help. I'm trying to perform a sum in my database, but here's my problem: it's not working 😊

The database consists of multiple rows, each movement of the product (reference) is displayed in a row with a start and end date/time. So, you can calculate the duration per line. Now, it's possible that the product appears multiple times in one department; this is an internal movement.

 

Now, I want to calculate an average per month of the duration. However, I need the sum per product (reference), and I want to report this by department. So, it would be great if this could be specified in the DAX code.

The example is for one department, but you can name the column "department." Different departments A, B, etc. The table below is a small extract, it shows what the outcome should be when you sum up the duration by reference.

 

Referencedepartment_namestart dateend dateDuration
1A19-5-2023 18:2419-5-2023 18:430:19
1A19-5-2023 18:4319-5-2023 19:471:04
2A3-5-2023 19:153-5-2023 19:190:04
2A3-5-2023 19:193-5-2023 21:181:59
2A3-5-2023 21:183-5-2023 21:220:04
3A11-5-2023 07:1511-5-2023 09:562:41
3A11-5-2023 09:5611-5-2023 10:170:21
4A1-5-2023 15:341-5-2023 17:091:35
4A1-5-2023 17:091-5-2023 18:050:56
4A1-5-2023 18:051-5-2023 18:080:03
5A1-5-2023 01:431-5-2023 01:500:07
6A1-5-2023 09:201-5-2023 10:431:23
7A1-5-2023 00:081-5-2023 03:103:02
8A1-5-2023 00:451-5-2023 01:591:14
8A1-5-2023 01:591-5-2023 02:040:05
9A1-5-2023 02:001-5-2023 02:390:39
9A1-5-2023 02:391-5-2023 02:450:06
10A1-5-2023 03:021-5-2023 04:261:24
11A1-5-2023 03:421-5-2023 04:250:43
12A1-5-2023 05:341-5-2023 08:533:19
12A1-5-2023 08:531-5-2023 08:540:01
13A1-5-2023 08:491-5-2023 10:431:54
14A1-5-2023 09:191-5-2023 12:122:53
14A1-5-2023 12:121-5-2023 12:140:02
15A1-5-2023 11:531-5-2023 14:442:51
15A1-5-2023 14:441-5-2023 15:050:21
15A1-5-2023 15:051-5-2023 16:221:17
16A1-5-2023 09:541-5-2023 09:590:05
16A1-5-2023 09:591-5-2023 10:150:16
16A1-5-2023 10:151-5-2023 10:210:06
16A1-5-2023 10:211-5-2023 12:151:54
16A1-5-2023 12:151-5-2023 14:452:30
2 REPLIES 2
bdeleur
Helper II
Helper II

Hi @v-binbinyu-msft ,

 

Tnx for your answer. When I look at the outcome, I see that it does indeed nicely take the average. However, it takes the average of the multiple rows per reference. I would like it to look at the total sum and then calculate the average per month from this total sum. For example, reference 1 has 2 rows: 19 and 64 minutes. Together, this is 83 minutes. In the average measurement, this becomes 41.5. But I want the average over the month from the sum across all references.

And if I apply this code to my large file with multiple years and multiple departments, I get 'gaps' in my overview per month/year.

 

bdeleur_0-1701333673087.png

 

 

v-binbinyu-msft
Community Support
Community Support

Hi @bdeleur ,

Please try below steps:

1. add a new column with below dax formula

 

Duration = DATEDIFF([start date],[end date],MINUTE)

 

2. create two measure with below dax formula

 

Total Duration by Product = 
   SUMX(
       SUMMARIZE(
           'Table',
           'Table'[Reference],
           'Table'[department_name],
           "TotalDuration", SUM('Table'[Duration])
       ),
       [TotalDuration]
   )
Average Duration per Month =
VAR cur_re =
    SELECTEDVALUE ( 'Table'[Reference] )
VAR cur_dt =
    SELECTEDVALUE ( 'Table'[department_name] )
VAR _a =
    MONTH (
        CALCULATE (
            MAX ( 'Table'[start date] ),
            FILTER ( ALL ( 'Table' ), [Reference] = cur_re && [department_name] = cur_dt )
        )
    )
VAR _b =
    FILTER (
        ALL ( 'Table' ),
        [Reference] = cur_re
            && [department_name] = cur_dt
            && MONTH ( [start date] ) = _a
    )
RETURN
    AVERAGEX ( _b, [Total Duration by Product] )

 

vbinbinyumsft_0-1701238723936.png

Please refre the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.