Forum Discussion

Ilse_ScpDt's avatar
Ilse_ScpDt
Icon for Helper I rankHelper I
4 years ago
Solved

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.

 

  • 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.

2 Replies

  • 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.
  • v-jingzhang's avatar
    v-jingzhang
    Icon for Community Support rankCommunity 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.