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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Creating new measure column groupby existing column in table

Hi,

 I have to acomplish a requirement where I need to calculate transport cost per KM travelled (Transport cost/KM travelled) group by month and country. As per the data I have 3 rows for each month where as Transport cost value is in transport cost colmn  Row 1 and KM travelled is the summation of row2 and row 3 from KM travelled column. Is there any way to achive this?

 

month      Country        Transport Cost           KM travelled       transport cost per KM

 Jun            USA                 A                               NULL                         A/(B+C)

 Jun            USA               NULL                             B                       

 Jun            USA               NULL                             C

 

Regards,

Rakesh

 

Regards,

Rakesh

1 ACCEPTED SOLUTION
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

As the sample data and expect result you shared, we can create a column using following DAX:

 

transort cost per KM =
VAR m = [month]
VAR c = [Country]
VAR t =
    FILTER ( 'Table', AND ( 'Table'[month] = m, 'Table'[Country] = c ) )
VAR cost =
    SUMX ( t, [Transprot Cost] )
VAR travelled =
    SUMX ( t, [KM travelled] )
RETURN
    IF (
        AND ( [Transprot Cost] <> 0, NOT ISBLANK ( [Transprot Cost] ) ),
        cost / travelled,
        BLANK ()
    )

 

Creating-new-measure-column-groupby-existing-column-in-table-1.png

 

But if you want a measure using in visual, we can create this measure using similar logic.

 

measure = 
VAR m =
    SELECTEDVALUE ( 'Table'[month] )
VAR c =
    SELECTEDVALUE ( 'Table'[Country] )
VAR t =
    FILTER ( ALL ( 'Table' ), AND ( 'Table'[month] = m, 'Table'[Country] = c ) )
VAR cost =
    SUMX ( t, [Transprot Cost] )
VAR travelled =
    SUMX ( t, [KM travelled] )
RETURN
cost / travelled

Creating-new-measure-column-groupby-existing-column-in-table-2.png

 

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ DongLi
If this post helps, then please consider Accept it as the solution to help the other members find it more

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

As the sample data and expect result you shared, we can create a column using following DAX:

 

transort cost per KM =
VAR m = [month]
VAR c = [Country]
VAR t =
    FILTER ( 'Table', AND ( 'Table'[month] = m, 'Table'[Country] = c ) )
VAR cost =
    SUMX ( t, [Transprot Cost] )
VAR travelled =
    SUMX ( t, [KM travelled] )
RETURN
    IF (
        AND ( [Transprot Cost] <> 0, NOT ISBLANK ( [Transprot Cost] ) ),
        cost / travelled,
        BLANK ()
    )

 

Creating-new-measure-column-groupby-existing-column-in-table-1.png

 

But if you want a measure using in visual, we can create this measure using similar logic.

 

measure = 
VAR m =
    SELECTEDVALUE ( 'Table'[month] )
VAR c =
    SELECTEDVALUE ( 'Table'[Country] )
VAR t =
    FILTER ( ALL ( 'Table' ), AND ( 'Table'[month] = m, 'Table'[Country] = c ) )
VAR cost =
    SUMX ( t, [Transprot Cost] )
VAR travelled =
    SUMX ( t, [KM travelled] )
RETURN
cost / travelled

Creating-new-measure-column-groupby-existing-column-in-table-2.png

 

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ DongLi
If this post helps, then please consider Accept it as the solution to help the other members find it more

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Thank you so much for the solution very informative.

 

 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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