Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
zebra
Helper II
Helper II

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 10
FreemanZ
Super User
Super User

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:

FreemanZ_0-1698219354951.png

 

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

zebra_0-1698239129307.png

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

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"
)

FreemanZ_0-1698304920421.png

 

wdx223_Daniel
Super User
Super User

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"
        )
    )

@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.

 

zebra_0-1698256697826.png

 

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

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

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

 

the above M code gives me the following error

Expression.Error: A cyclic reference was encountered during evaluation.

@zebra probably because you are trying to add new column with this code. Don't do that. Imagine you have your product table in PQ as separate query named "your_product_table". Then create a blank query with my code and see what happens. Check this video created by @ImkeF especially for you. 

sorry, @zebra , here is video 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors