Search code examples
powerbi

How to count a measure?


I'm having a problem and would like to know if you can help me again. I have a table as follows:

Date Data Measure
23/05/24 126 0.41
24/05/24 356 0.50
25/05/24 789 -0.33
26/05/24 568 -0.04
27/05/24 4785 0.10
28/05/24 25 -0.02
29/05/24 365 -0.12
30/05/24 1 -0.40
31/05/24 55 0.30
01/06/24 364 0.56

The second column contains some data I have, and the third one is a measure created from the data. I need to create a measure that gives me, for each day, how many of the values in the "Measure" column in the last 7 days (including the current day) are below -0.10. The result would be something like:

Date Data Measure New_measure
23/05/24 126 0.41 0
24/05/24 356 0.50 0
25/05/24 789 -0.33 1
26/05/24 568 -0.04 1
27/05/24 4785 0.10 1
28/05/24 25 -0.02 1
29/05/24 365 -0.12 2
30/05/24 1 -0.40 3
31/05/24 55 0.30 3
01/06/24 364 0.56 2

Additionally, these data must respond to some slicers I have with filters.

What I tried was something like:

New_measure = 
VAR aux_table = ADDCOLUMNS(
                    SUMMARIZE(
                    ALLSELECTED('table'),
                    'table'[Date]),
                    "Measure_aux", [Measure])
VAR max_day = MAX([Date])
 
RETURN
CALCULATE(
    COUNTROWS(
        FILTER(
            aux_table,
            [Measure_aux] < -0.1 &&
            [Date] >= max_day-6 &&
            [Date] <= max_day
        )
    )
)

But it doesn't work correctly. Does anyone have any idea how I can do this or what I did wrong?


Solution

  • Try modifying this line:

    VAR aux_table = ADDCOLUMNS(
                        SUMMARIZE(
                        ALLSELECTED('table'),
                        'table'[Date]),
                        "Measure_aux", [Measure])
    

    To:

    VAR aux_table =
      SUMMARIZE(
        ALLSELECTED('table'),
        'table'[Date],
        "Measure_aux", [Measure]
      )