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
Ilse_ScpDt
Helper I
Helper I

Calculate difference per timeslot

Hi there,

 

I am only familiair with simple dax so I am not sure how to calculate the following:

 

The difference between two values (a measure and a column) for every timeslot.

 

The thing is, this measure is calculating PM_Needed per timeslot if you put this together in a table. 

 

TimeSlotPM_NeededPM_PresentPM_Diff
07:30110
07:45110
08:00121
08:15220
08:30220
08:45231
09:00330
09:15330
09:30330
09:45330
10:00330
10:15330
10:30330
10:45330
11:00330
11:15330
11:30330
11:45330
12:00330
12:15330
12:30330
12:45330
13:0032-1
13:1532-1
13:30330
13:4532-1
14:0032-1
14:15330
14:3032-1
14:4532-1
15:00330
15:15330
15:30330
15:45330
16:00330
16:15330
16:30330
16:45330
17:00330
17:15330
17:30231
17:45121
18:00110
18:15110
18:30110
Total3115112

 

Now I would like to have 2 measures:

1. calculating the positive difference, in this case resulting in 4.

2. calculating the negative difference, in this case resulting in -6.

 

Timeslot is coming from Dim_Time and showing every minute.

PM_needed is based on a table with data per 15 minutes.

PM_present is a column from Fact_Staff.

 

Hope it is clear this way.

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @Ilse_ScpDt 

 

You need to create a variable table in the measure to get the PM_Diff for every Timeslot. The variable table should be in the format of the sample table you showed in the original post. Then filter the variable table and count its positive and negative differences. 

 

Below are sample codes for your reference. The difficulty might be at creating the variable table. You may need to modify your code for PM_Needed, PM_Present and PM_Diff according to your model. 

Positive =
VAR vTable =
    ADDCOLUMNS (
        SUMMARIZE (
            'Dim_Time',
            'Dim_Time'[TimeSlot],
            "__PMNeeded", [PM_Needed],
            "__PMPresent", SUM ( 'Fact_Staff'[PM_present] )
        ),
        "__PMDiff", [__PMPresent] - [__PMNeeded]
    )
RETURN
    COUNTROWS ( FILTER ( vTable, [__PMDiff] > 0 ) )
Negative =
VAR vTable =
    ADDCOLUMNS (
        SUMMARIZE (
            'Dim_Time',
            'Dim_Time'[TimeSlot],
            "__PMNeeded", [PM_Needed],
            "__PMPresent", SUM ( 'Fact_Staff'[PM_present] )
        ),
        "__PMDiff", [__PMPresent] - [__PMNeeded]
    )
RETURN
    COUNTROWS ( FILTER ( vTable, [__PMDiff] < 0 ) )

 

Hope this helps.

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @Ilse_ScpDt 

 

You need to create a variable table in the measure to get the PM_Diff for every Timeslot. The variable table should be in the format of the sample table you showed in the original post. Then filter the variable table and count its positive and negative differences. 

 

Below are sample codes for your reference. The difficulty might be at creating the variable table. You may need to modify your code for PM_Needed, PM_Present and PM_Diff according to your model. 

Positive =
VAR vTable =
    ADDCOLUMNS (
        SUMMARIZE (
            'Dim_Time',
            'Dim_Time'[TimeSlot],
            "__PMNeeded", [PM_Needed],
            "__PMPresent", SUM ( 'Fact_Staff'[PM_present] )
        ),
        "__PMDiff", [__PMPresent] - [__PMNeeded]
    )
RETURN
    COUNTROWS ( FILTER ( vTable, [__PMDiff] > 0 ) )
Negative =
VAR vTable =
    ADDCOLUMNS (
        SUMMARIZE (
            'Dim_Time',
            'Dim_Time'[TimeSlot],
            "__PMNeeded", [PM_Needed],
            "__PMPresent", SUM ( 'Fact_Staff'[PM_present] )
        ),
        "__PMDiff", [__PMPresent] - [__PMNeeded]
    )
RETURN
    COUNTROWS ( FILTER ( vTable, [__PMDiff] < 0 ) )

 

Hope this helps.

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Ilse_ScpDt
Helper I
Helper I

Now using this to sum PM_needed:

 

SumPM_Needed =
SUMX(
    FILTER(
          ALL(DIM_Time),
          DIM_Time[TimeSlot] <= MAX(DIM_Time[TimeSlot])
      ),
    [PM_Needed]
)
 
Still wondering how to get the positive and negative differences separately.

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.