Is there a way to make a variable in Power BI contain a dynamical table?

1k Views Asked by At

I am struggling with this measure for my Y-axis. I am trying to create a dynamic virtual table for the periodtype, however something is not working.

In my return statement, it won't allow me to select the columns in my virtual table _tbl, however I can select the table for the minx function.

I'm wondering if Power BI allow virtual tables to be dynamically based on a selected value.

Is there a way to make it happen?

My goal is to avoid creating 4 variables (1 for each dateperiod type) that have to be loaded each time the measure is used and instead only load the virtual table relevant for the selected datetype.

_MinRangeYAxis = 

VAR PeriodType = SELECTEDVALUE('DatePeriod'[Type] ) 
VAR SelectedKPI = SELECTEDVALUE('KPITable'[KPI] )


VAR _tbl = SWITCH(
        TRUE(), 
        PeriodType = "Date", ADDCOLUMNS (
                    SUMMARIZE (
                        'Calendar',
                        'Calendar'[CalendarDate]
                                ),
                                "VARAct", [_localKPISelectorActuals],
                                "VARBen", [_localKPISelectorBenchmark],
                                "VARTag", [_localKPISelectorTarget]
                            ),
        PeriodType = "Week", ADDCOLUMNS (
                    SUMMARIZE (
                        'Calendar',
                        'Calendar'[WeekKey]
                    ),
                                "VARAct", [_localKPISelectorActuals],
                                "VARBen", [_localKPISelectorBenchmark],
                                "VARTag", [_localKPISelectorTarget]
                            ),
        PeriodType = "Month", ADDCOLUMNS (
                    SUMMARIZE (
                        'Calendar',
                        'Calendar'[monthKey]
                    ),
                                "VARAct", [_localKPISelectorActuals],
                                "VARBen", [_localKPISelectorBenchmark],
                                "VARTag", [_localKPISelectorTarget]
                            ),
        PeriodType = "Year", ADDCOLUMNS (
                    SUMMARIZE (
                        'Calendar',
                        'Calendar'[Year]
                    ),
                                "VARAct", [_localKPISelectorActuals],
                                "VARBen", [_localKPISelectorBenchmark],
                                "VARTag", [_localKPISelectorTarget]
                            )
)

    
Var Interval =
        SWITCH (
            TRUE(),
            SelectedKPI = "KPI1", XX,
            SelectedKPI = "KPI2", XX,
            SelectedKPI = "KPI3", XX,
            SelectedKPI = "KPI4", XX,
            SelectedKPI = "KPI5", XX,
            SelectedKPI = "KPI6", XX,
            SelectedKPI = "KPI7", XX,
            SelectedKPI = "KPI8", XX,
            SelectedKPI = "KPI9", XX,
            SelectedKPI = "KPI10",XX,   
            SelectedKPI = "KPI11",XX,
            BLANK ()
        )

RETURN

    SWITCH (
        true(), 
        If(
            (
                MINX(
                { 
                    MINX(_tbl, [VARAct] ),
                    MINX(_tbl, [VARBen] ),
                    MINX(_tbl, [VARTag] )
                },
                [Value] 
                )
            ) - Interval < 0, 
            0, 
            (
                MINX(
                { 
                    MINX(_tbl, [VARAct] ),
                    MINX(_tbl, [VARBen]),
                    MINX(_tbl, [VARTag] )
                },
                [Value]
                )
            ) - Interval
    
0

There are 0 best solutions below