Forum Discussion

Peter_Price's avatar
Peter_Price
Frequent Visitor
7 years ago
Solved

Power Query - how we create a measure based on a dataset while union it back to the original dataset

Hi Folks,   Wondering how we create a measure based on a dataset <Not only specific to date column> while union it back to the original dataset ?   May be it make it easier to view the problem vi...
  • Zubair_Muhammad's avatar
    Zubair_Muhammad
    7 years ago

    Peter_Price

     

    And you can also use a DAX Calculated Table as well.

    Assuming your Table Name is Table1.

    From Modelling Tab >> New Table

    File attached as well

     

    Calculated Table =
    VAR Rows_I_need =
        GENERATE (
            SELECTCOLUMNS ( VALUES ( Table1[Category] ), "Mycategory", [Category] ),
            CALCULATETABLE ( TOPN ( 1, Table1, [Date], DESC ) )
        )
    VAR Add_Volumn_Difference =
        ADDCOLUMNS (
            Rows_I_need,
            "Difference", [Volume]
                - CALCULATE (
                    SUM ( Table1[Volume] ),
                    TOPN (
                        1,
                        FILTER (
                            Table1,
                            Table1[Category] = [Mycategory]
                                && Table1[Date] < EARLIER ( [Date] )
                        ),
                        [Date], DESC
                    )
                )
        )
    VAR ComparisonTable =
        SELECTCOLUMNS (
            Add_Volumn_Difference,
            "Date", [Date],
            "Category", [Category],
            "Calc Type", "Comparison",
            "Volumn", [Difference]
        )
    RETURN
        UNION ( Table1, ComparisonTable )