Forum Discussion

NYCDOT's avatar
NYCDOT
Regular Visitor
10 months ago
Solved

I"m issue with power bi dax code with error cannot find name

Please help. Here is the code:

Weekday_comparison_table =
VAR Weekdays =
                SELECTCOLUMNS(
                    GENERATESERIES(1, 7)
                    ,"WeekdayNumber", [Value]
                )
VAR AddWeekdayName =
                    ADDCOLUMNS(
                        weekdays,
                        "Weekday", SWITCH(
                            [WeekdayNumber],
                            "1", "Sunday",
                            "2", "Monday",
                            "3", "Tuesday",
                            "4", "Wednesday",
                            "5", "Thursday",
                            "6", "Friday",
                            "7", "Saturday",
                            "Unknown"
))

VAR AddMeasures =
                ADDCOLUMNS(
                    AddWeekdayName,
                    "Current Year",
                    CALCULATE(
                        [Total_Revenue],
                        'Calendar'[Day Name] = [WeekdayNumber],
                        SAMEPERIODLASTYEAR(DATEADD('Calendar'[date], 1, YEAR))
                    ),
                    "Previous Year", CALCULATE(
                        [Total_Revenue],
                        'Calendar'[Weekday num] = [WeekdayNumber],
                         SAMEPERIODLASTYEAR('Calendar'[date])
                    )
                )
VAR AddCalculations =
                ADDCOLUMNS(
                    AddMeasures,
                    "Variance", [Current Year] - [Previous Year],
                    "% Change", DIVIDE([Current Year] - [Previous Year], [Previous Year], 0)
)
RETURN AddCalculations
  • Hii NYCDOT 

     

    The error occurs because WeekdayNumber is a virtual column created by SELECTCOLUMNS() and exists only in row context. Inside CALCULATE(), filters are evaluated in filter context, so the column cannot be resolved by name. The fix is to store [WeekdayNumber] in a variable and use that variable inside CALCULATE().

3 Replies

  • Hii NYCDOT 

     

    The columns created inside ADDCOLUMNS() cannot be referenced by name within the same ADDCOLUMNS() call. [Current Year] and [Previous Year] don’t exist yet when Variance is evaluated. The fix is to use variables inside the same row context or calculate variance in a separate ADDCOLUMNS step. Also ensure you compare numeric weekday columns, not text names.

    • NYCDOT's avatar
      NYCDOT
      Regular Visitor

      Thank you for replying. I'm actually having problem with the WeekdayNumber Column. I'm getting an error message state:  Column 'WeekdayNumber' cannot be found or may not be used in this expression.

      • rohit1991's avatar
        rohit1991
        Icon for Super User rankSuper User

        Hii NYCDOT 

         

        The error occurs because WeekdayNumber is a virtual column created by SELECTCOLUMNS() and exists only in row context. Inside CALCULATE(), filters are evaluated in filter context, so the column cannot be resolved by name. The fix is to store [WeekdayNumber] in a variable and use that variable inside CALCULATE().