Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Dynamic flag with a date slicer

Hi community,


Is it possible to create a dynamic column or something with the same functionality? I need this to flag the rows in a table dinamically depending on a date slicer. 

 

So I have a table with 3 columns: Store, Date and value. Here a simplified example:

StoreDateValue
A01/01/20191
A03/03/20192
A05/05/20193
B02/02/20194
B04/04/20195
B08/08/20196

 

For all my calculations I need to use only the values in the last date for each store. So I first created a column that flags the last date for each row:

 

StoreDateValueFlag
A01/01/201910
A03/03/201920
A05/05/201931
B02/02/201940
B04/04/201950
B08/08/201961

 

The problem here is that this is an static calculation and what I need is this flag to be referenced to a date slicer in the dashboard, so that if the slicer is set to 06/06/2019 for example, the flag would look like this:

 

StoreDateValueFlag
A01/01/201910
A03/03/201920
A05/05/201931
B02/02/201940
B04/04/201951
B08/08/201960

 

I need the flag to then make some calculations like these:

KPI =
CALCULATE(
        SUM(Value)
        Filter(Table; Flag=1))

The way the flag is made right now, it considers only the last visit for each store for the current date, but when I go back in time with the slicer it filters the data instead of recalculating the flag.

 

Is there any way to get this?

I already tried building a table with groupby so that I have the last date for each store but again the same problem, the table is static and doesn't update with the slicer

 

NOTE = The real data set contains thousands of stores and dates, so any "manual" solution is not useful.

 

Thank you in advance for your time and help!

  • Mariusz's avatar
    Mariusz
    6 years ago

    Hi Anonymous 

     

    Try something like this.

     

    filter = 
    VAR __maxSelectedDate = 
        GROUPBY(
            CALCULATETABLE(
                FILTER( 
                    'Table',
                    'Table'[Date] <= MAX( 'Dates'[Date] ) 
                ),
                ALLEXCEPT( 'Table', 'Table'[Store] ) ),
            'Table'[Store],
            "@maxDate", MAXX( CURRENTGROUP(), 'Table'[Date] )
        ) 
    RETURN 
        CALCULATE( 
            COUNTROWS( 'Table' ),
            KEEPFILTERS( 
                TREATAS( __maxSelectedDate, 'Table'[Store], 'Table'[Date] )
            )
        )

     

    Sum of filter = 
    CALCULATE( 
        SUM( 'Table'[Value] ), 
        FILTER( 'Table' , [filter] ) 
    )
    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

     

10 Replies

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

    Hi Anonymous 

     

    You can create a measure like below.

    Flag = 
    VAR __selectedDate = MAX( Dates[Date] )
    RETURN 
    INT( 
        MAX( 'Table'[Date] ) = 
        CALCULATE(  
            MAX( 'Table'[Date] ),
            'Table'[Date] <= __selectedDate 
        )
    )

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Mariusz !

       

      This measure gets the desired result. However I need the flag to use it as a filter in further calculations. Imagine a simple one, the sum of 'Value' (for the last date on each store). If the flag was a column we would do something like this:

      CALCULATE(
           SUM(Table[Value]);
           FILTER(Table; Table[Flag]=1))

      How can I filter in this case as this flag is a measure?

      Thanks!

       

       

      • saraMissBI's avatar
        saraMissBI
        Icon for Resolver I rankResolver I

        Hi Anonymous ,

         

        You can directly create a quick measure: 

  • Anonymous's avatar
    Anonymous
    Not applicable

    Woow, that is excelent solution! :-).

    One question thought, is there a way to use it with the line chart visual?