Forum Discussion

zebra's avatar
zebra
Helper II
2 years ago

Creating custom column or measure

Hey All,

 

I am having hard time in creating custom column/measure according to following scnario. Suppose I have product table as follows

 

Idparent_idNamePriceUpdated_At
1nullp110013/10/2023
21p115014/10/2023
3nullp220015/10/2023
4nullp310010/10/2023
54p35011/10/2023
64p37012/10/2023

 

The Product table has products with its various revision according to price change. I want to make another column thats shows the trend.  The final output would be as follows.

 

Idparent_idNamePriceUpdated_AtTrend
1nullp110013/10/2023increasing
21p115014/10/2023 
3nullp220015/10/2023unchanged
4nullp310010/10/2023increasing
54p35011/10/2023 
64p37012/10/2023 

 

 

Here you can see that for p1, we have its price changed in 2nd row so the trend is increasing. for p2,  there are no revisions so trens is unchanged and for p3, we have two variations and here we need to compare last two and according to it, price is increasing so we have increasing trend. 

Would anyone help me in calculating these using custom columns?

 

Thanks 

zeebee

10 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    This code adds a calculated column

    Trend =
    VAR _tbl =
        FILTER (
            SUMMARIZE (
                'DataTable',
                'DataTable'[Name],
                'DataTable'[Price],
                'DataTable'[Updated_At]
            ),
            'DataTable'[Name] = EARLIER ( 'DataTable'[Name] )
        )
    VAR _dif =
        MAXX (
            INDEX ( -1, _tbl, ORDERBY ( 'DataTable'[Updated_At] ) ),
            'DataTable'[Price]
        )
            - MAXX (
                INDEX ( -2, _tbl, ORDERBY ( 'DataTable'[Updated_At] ) ),
                'DataTable'[Price]
            )
    RETURN
        IF (
            ISBLANK ( 'DataTable'[parent_id] ),
            SWITCH (
                TRUE (),
                OR ( COUNTROWS ( _tbl ) = 1, _dif = 0 ), "Uncahged",
                _dif > 0, "Increasing",
                "Decreasing"
            )
        )

    • zebra's avatar
      zebra
      Helper II

      thanks, this is working, can you provide me M code also so i can make the custom column using query editor?

      • AlienSx's avatar
        AlienSx
        Super User

        HI, zebra here goes M

        let
            Source = your_product_table,
            values = {"decreasing", "unchanged", "increasing"},
            f = (tbl as table) as table =>
                [prices = List.LastN(tbl[Price], 2),
                comp = Value.Compare(prices{1}? ?? prices{0}, prices{0}) + 1,
                add = Table.AddColumn(tbl, "Trend", (x) => if x[parent_id] = null then values{comp} else null)][add],
            group = Table.Group(Source, {"Name"}, {{"all", each f(Table.Sort(_, "Updated_At"))}}),
            expand = Table.ExpandTableColumn(group, "all", {"Id", "parent_id", "Price", "Updated_At", "Trend"})
        in
            expand

         

    • zebra's avatar
      zebra
      Helper II

      wdx223_Daniel , The above solution works however It is not working in the following scenario. I have another type of input table as follows. In this, I need to calculate trend based on step, there are finite number of steps lets say step1, step2, step3 and step 4.  and if step is changing, it is either increasing or decreasing and if there is only one row then it is unchanged. 

      Idparent_idother columnsstepUpdated_At
      1nullstep 113/10/2023
      21step 214/10/2023
      3nullstep 115/10/2023
      4nullstep  110/10/2023
      54step 211/10/2023
      64step 112/10/2023

       

      and I want to have trend according to steps as follows

      Idparent_idother columnsstepUpdated_AtTrend
      1nullstep 113/10/2023step increasing
      21step 214/10/2023 
      3nullstep 115/10/2023unchanged
      4nullstep  110/10/2023 
      54step 211/10/2023step increasing
      64step 112/10/2023 

       

      Moroever, please provide the explanation of the code as I am new to this tool and unable to understand how you are calculating. I try to use dax studio to see the output of the intermediate steps however dax studio throwing errors as shown below.

       

       

      So it will be great if you explain the steps and also let me know how i can see the output of intermediate steps?

       

      thanks

      zebee

  • hi zebra ,

     

    you may try to plot a table visual with the name column and a measure like:

    measure = 
    VAR _table = FILTER(data,data[Name]=MAX(data[Name]))
    VAR lastprice =
    MAXX(
        TOPN(1,_table, data[Updated_At]),
        data[Price]
    )
    VAR llastprice=
    MAXX(
        EXCEPT(
            TOPN(2, _table, data[Updated_At]),
            TOPN(1, _table, data[Updated_At])
        ),      
        data[Price]
    )
    RETURN
    SWITCH(
        TRUE(),
        llastprice = BLANK() || lastprice=llastprice, "Unchanged",
        lastprice>llastprice, "Increasing",
        "decreasing"
    )

    it worked like:

     

    • zebra's avatar
      zebra
      Helper II

      This is not the correct solution as it gives me the following output when I added the data as follows

      I need a measure/column that gives the trend in the orignal data format

      • FreemanZ's avatar
        FreemanZ
        Super User

        hi zebra ,

        if you need to bring all the columns into the visual, tweak the code like:

        measure = 
        VAR _table = FILTER(ALL(data),data[Name]=MAX(data[Name]))
        VAR lastprice =
        MAXX(
            TOPN(1,_table, data[Updated_At]),
            data[Price]
        )
        VAR llastprice=
        MAXX(
            EXCEPT(
                TOPN(2, _table, data[Updated_At]),
                TOPN(1, _table, data[Updated_At])
            ),      
            data[Price]
        )
        RETURN
        SWITCH(
            TRUE(),
            llastprice = BLANK() || lastprice=llastprice, "Unchanged",
            lastprice>llastprice, "Increasing",
            "decreasing"
        )