daterangeqliksenseset-analysis

Limit KPI by time range in set analysis using variables


I want to be able to limit a KPI, let's say turnover, to a specific time range which I chose by setting two variables: the lower bound and the upper bound.

I have created two tables, which I derive from the calendar-table.

month_start:
LOAD MonthYear as MonthStart
RESIDENT Calendar;

month_end:
LOAD MonthYear as MonthEnd
RESIDENT Calendar;

I create two filters, one for MonthStart and one for MonthEnd.

I set two variables. I know that GetFieldSelections()returns a string so I convert it back to a date.

=date#(GetFieldSelections(MonthEnd), 'MM YYYY')

=date#(GetFieldSelections(MonthStart), 'MM YYYY')

Finally I use this expression to calculate the KPI:

SUM({$< MonthYear = {"<$(=vEndMonth)>=$(=vStartMonth)"}>} [turnover])

But it doesn't work. I get 0,00€.

What am I doing wrong? What am I missing?

EDIT:

Example Data:

LOAD *
Inline [
%date, country, turnover
01.01.2021, DE, 1000
10.01.2021, AT, 2000
23.01.2021, CH, 1500
12.02.2021, DE, 2300
23.02.2021, DE, 5000
02.02.2021, CH, 1200
09.03.2021, AT, 3000
10.03.2021, CH, 1000
31.03.2021, DE, 3400
01.04.2021, CH, 2200
]

There is a calendar attached to the %date-field with the MonthYear-field which is created as monthname(%date) as MonthYear.

As result I want a Pivot Table with country as row-dimension and MonthYear as column. The measure is a master element with above formula.


Solution

  • Turned out my set expression was wrong.

    This works:

    Sum(
        {$<
            %date = {">=$(MonthStart)<$(MonthEnd)"}
        >}
    [turnover])
    

    Why? I had to remove the = inside the $(=...)

    As I defined my variable as

    =date#(GetFieldSelections(MonthEnd), 'MM YYYY')
    

    it was already evaluated in place due to the =-sign and returned a date.

    So when using $(=vEndMonth) in my set expression I tried to evaluate a date and I tried it the wrong way at that.

    If my variable didn't evaluate in place I had to do it in the set expression.

    So the other solution would have been:

    Variables like (without =-sign):

    date#(GetFieldSelections(MonthEnd), 'MM YYYY')
    
    date#(GetFieldSelections(MonthStart), 'MM YYYY')
    

    And set expression like:

    Sum(
        {$<
            %date = {">=$(=$(MonthStart))<$(=$(MonthEnd))"}
        >}
    [turnover])