Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Running total sum with a current hour flag column

Hello I have a table called FLA with applications per state with time and date columns. There is also a current_hour_flag column that shows 1 if the time is the current hour. For instance, the last row has a current hour flag value of 1 because 10:00 am was the current hour. 

This is my runningtotal measure that sums up all of the applicationcount from the previous day to each hour. 

RT2 = CALCULATE(
    SUM('FLA'[ApplicationCount])
    ,FILTER(
            ALLSELECTED(FLA),
             FLA[Time] <= MAX(FLA[Time])
              && FLA[Date] = MAX(FLA[Date])
          )
     )

 
My issue is that with the current hour flag column, it only returns the application count at that hour, rather than the sum of the day's application counts up to that hour. For instance, the measure returns 141 rather than 768 (Sum from 12:00 am to 10:00 am, please refer to first screenshot) 

141 rather than intended 768.

Thanks in advance.

  • Hi @aaronzheng 

     

    I tested my theory this morning and, if I understand what you are wanting which is to show all of the Current Hour Flags for the past n number of days, all you need to do is create a Calculated Column that says to return the current date and time if it equals the Current Hour Flag. 

     

    col_Current_Hour_Flag = IF ( Table1[Current_Hour_Flag] = 1 , Table1[ApplicationHourEST] , BLANK() )

     

    The output gives you the Current Hour and, when added to the visual with the measure I created in the earlier post being mea_Cumulative_Day, it gives you the total cumulative at the current hour for the "Current Hour Flag".  The new column uses the ApplicationHourEST and only presents a Date/Time if the Current Hour Flag is true and you use this new column as your X-Axis. Important, ensure you convert the output to Data/Type = Date/Time and then when you drag it in as the X-Axis on the visual, change it from Date Hierarchy to standard Date/Time.

     

    I did notice one thing in my previous mea_Cumulative_Day measure in that it didn't add the ApplicationCount between 00:00 to 01:00 or the ApplicationCount in the 09:00 parameter. The reason for the 09:00 is because it includes all counts > 09:00 and < 10:00.  But, to get the 503 count, I have modified my earlier measure to the below:

     

    mea_Cumulative_Day = 
    
    VAR _CurTime = MAX ( 'Table1'[ApplicationHourEST] )
    
    RETURN
    
        IF ( 
            TIMEVALUE ( _CurTime ) <> TIME ( 0 , 0 , 0 ) ,
                CALCULATE (
                    SUM ( Table1[ApplicationCount] ),
                    FILTER (
                        ALLSELECTED ( Table1 ),
                        AND ( Table1[ApplicationHourEST] >= DATEVALUE ( _CurTime ) , 'Table1'[ApplicationHourEST] <= _CurTime )
                    )
                ) , 
                CALCULATE (
                    SUM ( Table1[ApplicationCount] ) + 0 ,
                    FILTER (
                        ALLSELECTED ( Table1 ) ,
                        DATEVALUE ( Table1[ApplicationHourEST] ) = DATEVALUE ( _CurTime ) - 1 )
                    )
                )

     

     

     

     

     

    In the event that your "Current Hour Flag" is a measure, just convert the Calculated Column to a Measure.  In my data, I have set up the Current Hour Flag as a column, therefore, I've used this as the reason for creating a Calculated Column in this below example. 

     

    Please accept this as a solution if it is what you are after.

     

    Thanks,

    Theo

20 Replies

  • TheoC's avatar
    TheoC
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous,

     

    Can you please share the details of your Y and X Axis on the line graph? 

     

    Thanks,
    Theo

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Theo,

       

      The Y axis is my measure RT2 (shown above) and the X axis is ApplicationHourEST which is a date time.

      • TheoC's avatar
        TheoC
        Icon for Community Champion rankCommunity Champion

        Anonymous, thanks for that.  Can you add a Calculated Column that gives you a Current Day Flag (i.e. replicating your Current Hour Flag column but for the respective date).  That should provide you with a value of 1 for the respective day and align to the Hours for the day commencing at 00:00.  From there, modify your measure to SUM the values of the ApplicationCount using the Current Day Flag that equal 1 rather than the Current Hour Flag that is 1. 

         

        The Current Date Flag should be something as simple as "Current_Day_Flag = IF ( Table[ApplicationHourEST] = TODAY ( ) , 1 , 0 )".

         

        Hope that makes sense?

         

  • TheoC's avatar
    TheoC
    Icon for Community Champion rankCommunity Champion

    Anonymous yes, it should work as it's running a very similar logic to your current approach. 

     

    If it doesn't work, please share a data set. You can right click the Table in tabular view and press Copy Table and paste in Excel, de-identify, and upload that if it doesn't work 🙂

      • TheoC's avatar
        TheoC
        Icon for Community Champion rankCommunity Champion

        Hi Anonymous 

         

        Okay, so finally got to this. Apologies for the delay and it required a little more effort than I thought... The "m1" measure is representative of yours and the "mea_Cumulative2" is the new measure.  I've tried to keep the names of the columns as aligned as possible. 

         

        mea_Cumulative2 = 
        
        VAR _CurTime = MAX ( 'Table1'[ApplicationHourEST] )
        
        RETURN
        
            IF ( 
                TIMEVALUE ( _CurTime ) <> TIME ( 0 , 0 , 0 ) ,
                    CALCULATE (
                        SUM ( Table1[ApplicationCount] ),
                        FILTER (
                            ALLSELECTED ( Table1 ),
                            AND ( Table1[ApplicationHourEST] >= DATEVALUE ( _CurTime ) , 'Table1'[ApplicationHourEST] < _CurTime )
                        )
                    ) , 
                    CALCULATE (
                        SUM ( Table1[ApplicationCount] ) + 0 ,
                        FILTER (
                            ALLSELECTED ( Table1 ) ,
                            DATEVALUE ( Table1[ApplicationHourEST] ) = DATEVALUE ( _CurTime ) - 1 )
                        )
                    )

         

        Hope this is what you wanted! 🙂

         

         

  • TheoC's avatar
    TheoC
    Icon for Community Champion rankCommunity Champion

    Hi @aaronzheng 

     

    I tested my theory this morning and, if I understand what you are wanting which is to show all of the Current Hour Flags for the past n number of days, all you need to do is create a Calculated Column that says to return the current date and time if it equals the Current Hour Flag. 

     

    col_Current_Hour_Flag = IF ( Table1[Current_Hour_Flag] = 1 , Table1[ApplicationHourEST] , BLANK() )

     

    The output gives you the Current Hour and, when added to the visual with the measure I created in the earlier post being mea_Cumulative_Day, it gives you the total cumulative at the current hour for the "Current Hour Flag".  The new column uses the ApplicationHourEST and only presents a Date/Time if the Current Hour Flag is true and you use this new column as your X-Axis. Important, ensure you convert the output to Data/Type = Date/Time and then when you drag it in as the X-Axis on the visual, change it from Date Hierarchy to standard Date/Time.

     

    I did notice one thing in my previous mea_Cumulative_Day measure in that it didn't add the ApplicationCount between 00:00 to 01:00 or the ApplicationCount in the 09:00 parameter. The reason for the 09:00 is because it includes all counts > 09:00 and < 10:00.  But, to get the 503 count, I have modified my earlier measure to the below:

     

    mea_Cumulative_Day = 
    
    VAR _CurTime = MAX ( 'Table1'[ApplicationHourEST] )
    
    RETURN
    
        IF ( 
            TIMEVALUE ( _CurTime ) <> TIME ( 0 , 0 , 0 ) ,
                CALCULATE (
                    SUM ( Table1[ApplicationCount] ),
                    FILTER (
                        ALLSELECTED ( Table1 ),
                        AND ( Table1[ApplicationHourEST] >= DATEVALUE ( _CurTime ) , 'Table1'[ApplicationHourEST] <= _CurTime )
                    )
                ) , 
                CALCULATE (
                    SUM ( Table1[ApplicationCount] ) + 0 ,
                    FILTER (
                        ALLSELECTED ( Table1 ) ,
                        DATEVALUE ( Table1[ApplicationHourEST] ) = DATEVALUE ( _CurTime ) - 1 )
                    )
                )

     

     

     

     

     

    In the event that your "Current Hour Flag" is a measure, just convert the Calculated Column to a Measure.  In my data, I have set up the Current Hour Flag as a column, therefore, I've used this as the reason for creating a Calculated Column in this below example. 

     

    Please accept this as a solution if it is what you are after.

     

    Thanks,

    Theo

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello Theo, your calculated hour flag column solved my issue and I was able to use my previous measure. However, would you know how to adjust it so I could have a time slicer that would change the current hour to my selection?

      In other words, show the application count sum for the past 30 days at the hour I select.





      • TheoC's avatar
        TheoC
        Icon for Community Champion rankCommunity Champion

        Anonymous great to hear it worked, mate!  It was probably one of the more complex ones I've got to work on here in the Community! It is always fun to learn new things, so thank you for posting!

         

        I am not aware of a way to link the two due to the Current Hour Flag dictates the way in which the measure works to return them (i.e. it's progressive according to the current hour).

         

        However, I believe there may be a way whereby you can create a dynamic measure based on a Slicers selected values.  So, the way that you would want to establish the measure with with logic that can apply the following:

         

        Dynamic Measure = IF a user has selected an option from the Hour Slicer then present [Current Hour Measure] else present [Hour Measure]...

         

        Actually, I just found this post here which is very closely aligned with the above lol: https://community.powerbi.com/t5/Desktop/Dynamic-Measure-Values-Based-on-Slicer-Selection-with-Options/m-p/230151

         

        Anyway, if you're happy with the solution relative to this post, make sure to mark it as solved and many thanks for posting your topic. It's been fun!

  • Hi, Anonymous 

     



    My issue is that with the current hour flag column, it only returns the application count at that hour, rather than the sum of the day's application counts up to that hour. For instance, the measure returns 141 rather than 768 (Sum from 12:00 am to 10:00 am, please refer to first screenshot) 

    141 rather than intended 768.


    I don’t think you need to make any changes to your measures.

    If I understand your requirements correctly, you need to calculate the running total of the application count of the day until a certain hour.
    Your measures are exactly what you want. I did not make any changes to your measure, and I got the results that fully meet your needs.

     

    The screenshot above also shows 49 as the application count for CA on 10/9/2021 at 9:00 am, but I want it to show 510 as the sum of the application counts on 10/9/2021 until 9:00 am. 510 is the sum of the application counts (3rd column) shown below in this screenshot:

    1)--->768

    2)--->510

     

     

     

    Maybe you made a mistake in some operations, but your measure is completely correct.

    Please refer to the attachment below for details.  Hope this helps.

     

    Best Regards,
    Community Support Team _ Zeon Zheng


    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • TheoC's avatar
      TheoC
      Icon for Community Champion rankCommunity Champion

      v-angzheng-msft, if you read some of the other posts, the solution requires only the amount at the current hour to be shown. I have prepared the following which fits the requirements:

      _______

       

      Hi @aaronzheng 

       

      I tested my theory this morning and, if I understand what you are wanting which is to show all of the Current Hour Flags for the past n number of days, all you need to do is create a Calculated Column that says to return the current date and time if it equals the Current Hour Flag. 

       

      col_Current_Hour_Flag = IF ( Table1[Current_Hour_Flag] = 1 , Table1[ApplicationHourEST] , BLANK() )

       

      The output gives you the Current Hour and, when added to the visual with the measure I created in the earlier post being mea_Cumulative_Day, it gives you the total cumulative at the current hour for the "Current Hour Flag".  The new column uses the ApplicationHourEST and only presents a Date/Time if the Current Hour Flag is true and you use this new column as your X-Axis. Important, ensure you convert the output to Data/Type = Date/Time and then when you drag it in as the X-Axis on the visual, change it from Date Hierarchy to standard Date/Time.

       

      I did notice one thing in my previous mea_Cumulative_Day measure in that it didn't add the ApplicationCount between 00:00 to 01:00 or the ApplicationCount in the 09:00 parameter. The reason for the 09:00 is because it includes all counts > 09:00 and < 10:00.  But, to get the 503 count, I have modified my earlier measure to the below:

       

      mea_Cumulative_Day = 
      
      VAR _CurTime = MAX ( 'Table1'[ApplicationHourEST] )
      
      RETURN
      
          IF ( 
              TIMEVALUE ( _CurTime ) <> TIME ( 0 , 0 , 0 ) ,
                  CALCULATE (
                      SUM ( Table1[ApplicationCount] ),
                      FILTER (
                          ALLSELECTED ( Table1 ),
                          AND ( Table1[ApplicationHourEST] >= DATEVALUE ( _CurTime ) , 'Table1'[ApplicationHourEST] <= _CurTime )
                      )
                  ) , 
                  CALCULATE (
                      SUM ( Table1[ApplicationCount] ) + 0 ,
                      FILTER (
                          ALLSELECTED ( Table1 ) ,
                          DATEVALUE ( Table1[ApplicationHourEST] ) = DATEVALUE ( _CurTime ) - 1 )
                      )
                  )

       

       

       

       

       

      In the event that your "Current Hour Flag" is a measure, just convert the Calculated Column to a Measure.  In my data, I have set up the Current Hour Flag as a column, therefore, I've used this as the reason for creating a Calculated Column in this below example. 

       

      Please accept this as a solution if it is what you are after.

       

      Thanks,

      Theo

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello, you were right that my measure was correct. Theo's current hour column did the trick and solved my issue. 

  • TheoC's avatar
    TheoC
    Icon for Community Champion rankCommunity Champion

    Hi @aaronzheng 

     

    I tested my theory this morning and, if I understand what you are wanting which is to show all of the Current Hour Flags for the past n number of days, all you need to do is create a Calculated Column that says to return the current date and time if it equals the Current Hour Flag. 

     

    col_Current_Hour_Flag = IF ( Table1[Current_Hour_Flag] = 1 , Table1[ApplicationHourEST] , BLANK() )

     

    The output gives you the Current Hour and, when added to the visual with the measure I created in the earlier post being mea_Cumulative_Day, it gives you the total cumulative at the current hour for the "Current Hour Flag".  The new column uses the ApplicationHourEST and only presents a Date/Time if the Current Hour Flag is true and you use this new column as your X-Axis. Important, ensure you convert the output to Data/Type = Date/Time and then when you drag it in as the X-Axis on the visual, change it from Date Hierarchy to standard Date/Time.

     

    I did notice one thing in my previous mea_Cumulative_Day measure in that it didn't add the ApplicationCount between 00:00 to 01:00 or the ApplicationCount in the 09:00 parameter. The reason for the 09:00 is because it includes all counts > 09:00 and < 10:00.  But, to get the 503 count, I have modified my earlier measure to the below:

     

    mea_Cumulative_Day = 
    
    VAR _CurTime = MAX ( 'Table1'[ApplicationHourEST] )
    
    RETURN
    
        IF ( 
            TIMEVALUE ( _CurTime ) <> TIME ( 0 , 0 , 0 ) ,
                CALCULATE (
                    SUM ( Table1[ApplicationCount] ),
                    FILTER (
                        ALLSELECTED ( Table1 ),
                        AND ( Table1[ApplicationHourEST] >= DATEVALUE ( _CurTime ) , 'Table1'[ApplicationHourEST] <= _CurTime )
                    )
                ) , 
                CALCULATE (
                    SUM ( Table1[ApplicationCount] ) + 0 ,
                    FILTER (
                        ALLSELECTED ( Table1 ) ,
                        DATEVALUE ( Table1[ApplicationHourEST] ) = DATEVALUE ( _CurTime ) - 1 )
                    )
                )

     

     

     

     

     

    In the event that your "Current Hour Flag" is a measure, just convert the Calculated Column to a Measure.  In my data, I have set up the Current Hour Flag as a column, therefore, I've used this as the reason for creating a Calculated Column in this below example. 

     

    Please accept this as a solution if it is what you are after.

     

    Thanks,

    Theo