Forum Discussion

aliahad's avatar
aliahad
Frequent Visitor
1 year ago
Solved

Trying to get Max value using a column parameter

I have a table where I have added a column with an IF statment

RowNumberYear = IF(VALUE(MAX('gt EMPLOYEE'[YEAR])) = VALUE('Head Count'[YEAR]), 1, 0)  

I am trying to get Max value using following expression:

RowNumber = 
var FilteredTable = 
FILTER('Head Count', 'Head Count'[RowNumberYear] = 1)
RETURN
MAX(FilteredTable[MONTH])

I am getting the following error message:

Table variable 'FilteredTable' cannot be used in current context because a base table is expected.

Any help is appreciated.

 

Thanks,

Ali.

  • Hi aliahad ,

    I think Irwan s approach was correct and solves the ask. However, to be specific, considering there is only month and year column, the solution will be as below.

    If this solves, Plz accept as solution

8 Replies

  • Irwan's avatar
    Irwan
    Super User

    hello aliahad 

     

    as far as my knowledge, MAX() only accept column that has been defined.

    DAX doesnt accept force command as my example above.

     

    instead, you can do as below.

     

    Hope this will help.

    Thank you.

     

    • aliahad's avatar
      aliahad
      Frequent Visitor

      Hi Irwan 

      The DAX expression you provided is giving me max value for all the rows.

      Column = CALCULATE(
                          MAX('Head Count'[MONTH]),
                          FILTER('Head Count', 'Head Count'[RowNumberYear] = 1)
      )   

      So in my dataset I have YEAR and MONTH, and I trying to get 1 or 0 in the calculated column for max year and month, so the current max month I got in the dataset is  5 and current max year is 2025, but when I new data for June I want current max month = 6 and current max year = 2025 and next month the max month will be July. So I am trying to create a flag using the calculated column.

       

      Thanks,

      Ali.

    • aliahad's avatar
      aliahad
      Frequent Visitor

      Hi Irwan ,

       

      I tried the expression you provided, it gives me the 'MAX' for all the rows in the dataset.

       

      So in my dataset I have a column 'MONTH' and 'YEAR' and I need to get a calculated column that gives me 1 for 'MAX' month and 'MAX' year, and 0 for all the remainder rows. The current dataset got max year as 2025 and max month as 5, and in my column I need 1 for MAX year 2025 and MAX month 5 and 0 for remainder of the rows. This should switch when the June data is added to the dataset.

       

      I hope I have clarify the ask.

       

      Thanks,

      Ali.

      • Irwan's avatar
        Irwan
        Super User

        hello aliahad 

         

        i assumed you want to get MAX value of your date (max month AND max year).

        Here is a simple example.

        Check =
        var _Max = MAX('Table'[Date])
        Return
        IF(
            'Table'[Date]=_Max,
            1,
            0
        )

         

        but if you need separate column between month and year, then please try this.


        Check Month =

        var _Max = MAX('Table'[Month])
        Return
        IF(
            MONTH('Table'[Date])=_Max,
            1,
            0
        )
        Check Year =
        var _Max = MAX('Table'[Year])
        Return
        IF(
            YEAR('Table'[Date])=_Max,
            1,
            0
        )

         

        Hope this will help.

        Thank you.

  • Hi aliahad,

    The error you're encountering in Power BI DAX, is due to the fact that you're trying to use a table variable (FilteredTable) directly inside a scalar function like MAX(), which expects a column reference from a base table, not a variable.


    Please try below DAX expression:
    RowNumber =
    CALCULATE(
    MAX('Head Count'[MONTH]),
    'Head Count'[RowNumberYear] = 1
    )

    Hope the above DAX expression may help you.

    Please let me know if you have further questions.

    If this reply helped solve your problem, please consider clicking "Accept as Solution" so others can benefit too. And if you found it useful, a quick "Kudos" is always appreciated, thanks! 

     

    Best Regards, 

    Maruthi 

    LinkedIn - http://www.linkedin.com/in/maruthi-siva-prasad/ 

    X            -  Maruthi Siva Prasad - (@MaruthiSP) / X

  • aliahad's avatar
    aliahad
    Frequent Visitor

    Hi Irwan ,

     

    I tried the expression you provided, it gives me the 'MAX' for all the rows in the dataset.

     

    So in my dataset I have a column 'MONTH' and 'YEAR' and I need to get a calculated column that gives me 1 for 'MAX' month and 'MAX' year, and 0 for all the remainder rows. The current dataset got max year as 2025 and max month as 5, and in my column I need 1 for MAX year 2025 and MAX month 5 and 0 for remainder of the rows. This should switch when the June data is added to the dataset.

     

    I hope I have clarify the ask.

     

    Thanks,

    Ali.

    • Rupak_bi's avatar
      Rupak_bi
      Super User

      Hi aliahad ,

      I think Irwan s approach was correct and solves the ask. However, to be specific, considering there is only month and year column, the solution will be as below.

      If this solves, Plz accept as solution