Forum Discussion

luccaguimaraes's avatar
luccaguimaraes
Frequent Visitor
4 years ago
Solved

How to create a Row Column in DAX

Hi,

 

I need to create a column that contains the row number, exactly like the red numbers on the print below:

 

*It has to be on DAX because It is a calculated table from M code

*It will always have 4 rows
*I do not intend to rank the rows by the name, only by normal order

 

 

Could someone help me?

  • Here is one way to do it with DAX (without RANKX). Just create a DAX table with the expression below.

     

    MonthTable =
    VAR fourmonthsago =
    EOMONTH( TODAY(), -4 )
    VAR monthdates =
    ADDCOLUMNS(
    GENERATESERIES( 1, 4, 1 ),
    "Mes/Ano", FORMAT( EOMONTH( fourmonthsago, [Value] ), "mmm/yy" )
    )
    RETURN
    monthdates

     

    Pat

     

2 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Here is one way to do it with DAX (without RANKX). Just create a DAX table with the expression below.

     

    MonthTable =
    VAR fourmonthsago =
    EOMONTH( TODAY(), -4 )
    VAR monthdates =
    ADDCOLUMNS(
    GENERATESERIES( 1, 4, 1 ),
    "Mes/Ano", FORMAT( EOMONTH( fourmonthsago, [Value] ), "mmm/yy" )
    )
    RETURN
    monthdates

     

    Pat

     

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    If you just need a 4-row/2-column table, you could just use the DATATABLE function.

    DATATABLE – DAX Guide

     

    Or, if it needs to be dynamic and show the last 4 months, you could calculate a Date column from the month/year and then use RANKX on that to get 1-4.

     

    Pat