0

How can I write this query in DAX ?

SELECT TimeID, Code,(SELECT SUM(qty) FROM cteRanked c2 WHERE c2.rownum <= c1.rownum and c2.Code=c1.Code)
  FROM cteRanked c1;

Thank you

1 Answers1

0

Considering that no grouping is specified in the SQL query, this might work

EVALUATE
SELECTCOLUMNS(
    ADDCOLUMNS(
        cteRanked,
        "QtyRT",
            VAR LastRowNum = cteRanked[rownum]
            RETURN
                CALCULATE(
                    SUMX( cteRanked, cteRanked[qty] ),
                    REMOVEFILTERS( cteRanked ),
                    VALUES( cteRanked[Code] ),
                    cteRanked[rownum] <= LastRowNum
                )
    ),
    "TimeID", cteRanked[TimeID],
    "Code", cteRanked[Code],
    "QtyRT", [QtyRT]
)

First we iterate over the whole cteRanked table, adding the column QtyRT (quantity Running Total), that contains the sum of all the rows with the same Code with a rownum <= of the saved rownum. Then we select just the required columns using SELECTCOLUMNS.

sergiom
  • 4,791
  • 3
  • 24
  • 32