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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
axa830
Frequent Visitor

Level of detail calculation after splitting into rows

Hello All,

 

I have a problem that I have been stuck on for the last week. My data set is as under:

AccountAccount DepartmentAccount SiteAccount BuildingYearAccount AreaAccount AffectsSales
Acount1Dept1Site1Building12021Area1a,b,c100
Acount2Dept2Site2Building22020Area2null200
Acount3Dept3Site3Building32021Area3a2000
Acount4Dept4Site4Building42020Area4a,b,c,d2000
Acount5Dept5Site5Building52021Area5d2000
Acount6Dept6Site6Building62021Area6null2000
Acount7Dept7Site7Building72020Area7null2000

 

What I did was split the column Account Affects into multiple rows by using transform and splitting into rows to get the resulting data set:

AccountAccount DepartmentAccount SiteAccount BuildingYearAccount AreaAccount AffectsSales
Acount1Dept1Site1Building12021Area1a100
Acount1Dept1Site1Building12021Area1b100
Acount1Dept1Site1Building12021Area1c100
Acount2Dept2Site2Building22020Area2null200
Acount3Dept3Site3Building32021Area3a2000
Acount4Dept4Site4Building42020Area4a2000
Acount4Dept4Site4Building42020Area4b2000
Acount4Dept4Site4Building42020Area4c2000
Acount4Dept4Site4Building42020Area4d2000
Acount5Dept5Site5Building52021Area5d2000
Acount6Dept6Site6Building62021Area6null2000
Acount7Dept7Site7Building72020Area7null2000

 

 

Now I am stuck in a problem as I have card and other visuals that need to be prepared. What I want to achieve is the total of sales based on all the slicers(all columns in the dataset except Sales).

 

So suppose if I have no filter selected my total would become (10,300 by summing 100 from Account1,200 from Account2 and so on) i.e take the total of the three rows that account 1 splits into and then divide by the count distinct of the Account Affects column which in this case would be 3(300/3=100) and then move on to the next Account and apply same logic.

 

But if from a slicer I select Account Afffects "a" then my total should be 5100(100 from Account1,2000 from Account3,and 2000 from Account4 for a total of 5100)

 

 

I have tried couple of different approaches but cant seem to find a solution. Please advise.

 

 

 

 

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @axa830 

Why don't you do that division by the number of rows directly in power query? It will be easier to operate in DAX later. Somethig like this. Place the following M code in a blank query to see the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdA7CsMwEATQu6hWIetbO+QGKY0LxxZBIJQQrPvHa49hlaQajYrH7g6D6OdnLWsnpLjG1563tEbKS015SeVBb600Rf+OE+Uk73LeslNKjPJENBANRDNEH4gCQrXUnPdfbhgYBoZhhmkHoTodABcsBAvBMsG2U9hzFbn8Og6Og+OY49pJqP4RPAQPwTPBt4Jv7sGRACQACQwJ7TrhGxk/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, #"Account Department" = _t, #"Account Site" = _t, #"Account Building" = _t, Year = _t, #"Account Area" = _t, #"Account Affects" = _t, Sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", type text}, {"Account Department", type text}, {"Account Site", type text}, {"Account Building", type text}, {"Year", Int64.Type}, {"Account Area", type text}, {"Account Affects", type text}, {"Sales", Int64.Type}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Split([Account Affects],",")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each [Sales]/List.Count([Custom]), type number),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom1", "Custom"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Account Affects", "Sales"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Account Affects"}, {"Custom.1", "Sales"}})
in
    #"Renamed Columns"

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

View solution in original post

1 REPLY 1
AlB
Super User
Super User

Hi @axa830 

Why don't you do that division by the number of rows directly in power query? It will be easier to operate in DAX later. Somethig like this. Place the following M code in a blank query to see the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdA7CsMwEATQu6hWIetbO+QGKY0LxxZBIJQQrPvHa49hlaQajYrH7g6D6OdnLWsnpLjG1563tEbKS015SeVBb600Rf+OE+Uk73LeslNKjPJENBANRDNEH4gCQrXUnPdfbhgYBoZhhmkHoTodABcsBAvBMsG2U9hzFbn8Og6Og+OY49pJqP4RPAQPwTPBt4Jv7sGRACQACQwJ7TrhGxk/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, #"Account Department" = _t, #"Account Site" = _t, #"Account Building" = _t, Year = _t, #"Account Area" = _t, #"Account Affects" = _t, Sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", type text}, {"Account Department", type text}, {"Account Site", type text}, {"Account Building", type text}, {"Year", Int64.Type}, {"Account Area", type text}, {"Account Affects", type text}, {"Sales", Int64.Type}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Split([Account Affects],",")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each [Sales]/List.Count([Custom]), type number),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom1", "Custom"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Account Affects", "Sales"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Account Affects"}, {"Custom.1", "Sales"}})
in
    #"Renamed Columns"

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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