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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
davidz106
Helper III
Helper III

DAX to M code (dividing within same column)

Hi. I have a following DAX code to calculate percentage of a component (by dividing quantity of component with total). Table fileds are (ID, quantity, factor and component). I use my DAX code as measure which works great as long as visualisation is based only on this table only but has certain problems when trying to visualize data from other related tables. 

 

 

 

Component_percentage = 
VAR Divide =
    CALCULATE (
        SUM ( 'Table1'[Quantity]] ),
        FILTER (
            ALLEXCEPT ( 'Table1', 'Table1'[ID] ),
            'Table1'[Component] = "Total"
        )
    )
RETURN
    IF (
        SUM ( 'Table1'[Factor] ) = 1,
        SUM ( 'Table1'[Quantity]] ) / Divide,
        0
    )

 

 

 

I could solve this by adding new column in M code instead of using DAX measure. However, I am having problems with defining it. Can you propose a solution?

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@davidz106 , In power query you have duplicate table, Filter it and the use group by- https://docs.microsoft.com/en-us/power-query/group-by

In that you ill get the total column

Merge the tables and you the line value and sub total

Divide into a new column 

 

 merge : https://radacad.com/append-vs-merge-in-power-bi-and-power-query

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

1 REPLY 1
amitchandak
Super User
Super User

@davidz106 , In power query you have duplicate table, Filter it and the use group by- https://docs.microsoft.com/en-us/power-query/group-by

In that you ill get the total column

Merge the tables and you the line value and sub total

Divide into a new column 

 

 merge : https://radacad.com/append-vs-merge-in-power-bi-and-power-query

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.