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
ngct1112
Post Patron
Post Patron

Power Query - Logical calculation from Text

Hi, I am trying to transform texts into number with caculations.

At this moment I need to use dozens of steps to process this caculations(split by ",", then conditional columns, then sum together). May I know is there any way it can be simplified in PowerQuery - PowerBi Desktop. Appreciated.

 

Assuming

A=4

B=1

C=1

(Front/Back)

 

Text columnResult
Front A4/0
Back A, Back C0/5
Front B, Back C1/1
Front A, Back A4/4
Front A, Front B, Front C, Back B6/1
Front A, Front A, Front B, Back B, Back C9/2

 

 

2 ACCEPTED SOLUTIONS

Hi @ngct1112 ,

I would suggest to merge and group.

 

Transform your numbers in to a suitable table (called "Table":

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJRitWJVnICsgzBLGcIKxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Character = _t, Number = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Character", type text}, {"Number", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "FB", each {"Front", "Back"}),
    #"Expanded FB" = Table.ExpandListColumn(#"Added Custom", "FB"),
    #"Inserted Merged Column" = Table.AddColumn(#"Expanded FB", "Text", each Text.Combine({[FB], [Character]}, " "), type text)
in
    #"Inserted Merged Column"

 

And then work out the calculations like so:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkmtKFFIzs8pzc1T0lEKSi0uzSlRitWJVnIrys8rUXAECproG4BFnBKTsxUcdRTAtDNQwkDfFEmpE5KMob4hsiFQGYhhJqgycM0QhjNUrRNQrRm6KZiaIGqRbLbUN1KKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Text column", type text}, {"Result", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each Text.Split([Text column], ", ")),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Merged Queries" = Table.NestedJoin(#"Expanded Custom", {"Custom"}, Table, {"Text"}, "Table", JoinKind.LeftOuter),
    #"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table", {"Number", "FB"}, {"Number", "FB"}),
    #"Grouped Rows" = Table.Group(#"Expanded Table", {"Text column", "FB"}, {{"Sum", each List.Sum([Number]), type nullable number}}, GroupKind.Local),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[FB]), "FB", "Sum", List.Sum)
in
    #"Pivoted Column"

 

This is fairly dynamic, so you can add calculation items as you like.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

Hi @ngct1112 ,

great to hear. 

Please mark my post as answer then 😉

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@ImkeF , Can you help on this.

 

Hi @ngct1112 ,

I would suggest to merge and group.

 

Transform your numbers in to a suitable table (called "Table":

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJRitWJVnICsgzBLGcIKxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Character = _t, Number = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Character", type text}, {"Number", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "FB", each {"Front", "Back"}),
    #"Expanded FB" = Table.ExpandListColumn(#"Added Custom", "FB"),
    #"Inserted Merged Column" = Table.AddColumn(#"Expanded FB", "Text", each Text.Combine({[FB], [Character]}, " "), type text)
in
    #"Inserted Merged Column"

 

And then work out the calculations like so:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkmtKFFIzs8pzc1T0lEKSi0uzSlRitWJVnIrys8rUXAECproG4BFnBKTsxUcdRTAtDNQwkDfFEmpE5KMob4hsiFQGYhhJqgycM0QhjNUrRNQrRm6KZiaIGqRbLbUN1KKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Text column", type text}, {"Result", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each Text.Split([Text column], ", ")),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Merged Queries" = Table.NestedJoin(#"Expanded Custom", {"Custom"}, Table, {"Text"}, "Table", JoinKind.LeftOuter),
    #"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table", {"Number", "FB"}, {"Number", "FB"}),
    #"Grouped Rows" = Table.Group(#"Expanded Table", {"Text column", "FB"}, {{"Sum", each List.Sum([Number]), type nullable number}}, GroupKind.Local),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[FB]), "FB", "Sum", List.Sum)
in
    #"Pivoted Column"

 

This is fairly dynamic, so you can add calculation items as you like.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@ImkeF I have applied this to my model and it working perfectly. Great thanks for your help!

Hi @ngct1112 ,

great to hear. 

Please mark my post as answer then 😉

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.