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

Create column based on variety of variables in multiple columns and rows

Hi Everyone,

 

I would like to add a column onto the table below which returns a different value depending on the operator that is available e.g. for:

  • KPI 635 the answer is 7800/2600 = 3.  This is the Balance Sum of KPI 1 divided by the Balance Sum of KPI 3
  • KPI 2393 the answer is 2500 - 2700 = -200. This is the Balance Sum of KPI 2 minus the Balance Sum of KPI 4

It's worth noting that:

  • KPI Number is a unique value
  • No KPI referenced in column A or B will not have a Balance Sum
  • Column A will always be the numerator and B the denominator for division
  • Formula1 for our purpose is = ((2 + 3) / 3)

 

The obvious way to me seems to be to use SWITCH to allow the column to go through all the different operators. The issue that I'm having is in using columns A & B to reference their respective KPIs Balance Sum.

 

I greatly appreciate you taking the time to read my issue and am open to any suggestions you might have.

 

KPI NumberKPI NameDepartmentBalance SumOperatorABColumn
1Total SalesOverall7800   7800
2Total SalesBody Shop2500   2500
3Total SalesPre-Delivery2600   2600
4Total SalesNew Vehicles2700   2700
635Sales per advisor FTEBody Shop /133
2393Unsold hoursService -24-200
715Total IncomeAdministration +235100
2372Uncharged at effective rateBody Shop *347020000
2373Total IncomeService Formula1231.666667

 

Sample Data:
Create Column Example PBI
Create Column Example Excel 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Ok. Try this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZBBa8MwDIX/ishxa1kTN013XNkKu2yDdLuEHoytNgYnKo6b0X8/OWk3FoOFJXif35OrKkmTWbIjLy2U0mLH03uPTlrLXbFeLPi6nv2sSrJIvSF9gbKmE/dZPtWLSP/hcP6M1rDJJSCrKbKMkDf8hi+sjRrHrJgiK5FzO8jhhA6k7k1HDra7l0lCPg9cYWkxLiQeQ8bPtiOroaazCxYlut4oHIF58ORaDkCR5r8BX1tFTVA96ca0pvNOekPtiN1fsZtPkQ0+qpbuiBqkBzwcUHn+CWAO46R3A30z5hdE7Pwv6ZZcc7Yy/XPe/wA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"KPI Number" = _t, #"KPI Name" = _t, Department = _t, #"Balance Sum" = _t, Operator = _t, A = _t, B = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}}),
    #"Aggiunta colonna personalizzata" = Table.AddColumn(#"Modificato tipo", "form", each try if [Operator]="Formula1" then Text.From([A]) & "/"& Text.From([B])&"+1" else #"Modificato tipo"[Balance Sum]{[A]-1} & [Operator] & #"Modificato tipo"[Balance Sum]{[B]-1} otherwise [Balance Sum]),
    #"Aggiunta colonna personalizzata1" = Table.AddColumn(#"Aggiunta colonna personalizzata", "expr", each Expression.Evaluate([form]))
in
    #"Aggiunta colonna personalizzata1"

 

 

This is a solution that fits the example you have proposed. I'm not sure it will work in more general cases. If you want a more robust solution, you should provide more comprehensive examples or better explain the structure of your data and the logic behind it.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZBBa8MwDIX/ishxa1kTN013XNkKu2yDdLuEHoytNgYnKo6b0X8/OWk3FoOFJXif35OrKkmTWbIjLy2U0mLH03uPTlrLXbFeLPi6nv2sSrJIvSF9gbKmE/dZPtWLSP/hcP6M1rDJJSCrKbKMkDf8hi+sjRrHrJgiK5FzO8jhhA6k7k1HDra7l0lCPg9cYWkxLiQeQ8bPtiOroaazCxYlut4oHIF58ORaDkCR5r8BX1tFTVA96ca0pvNOekPtiN1fsZtPkQ0+qpbuiBqkBzwcUHn+CWAO46R3A30z5hdE7Pwv6ZZcc7Yy/XPe/wA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"KPI Number" = _t, #"KPI Name" = _t, Department = _t, #"Balance Sum" = _t, Operator = _t, A = _t, B = _t]),
    mt = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}, {"Balance Sum", Int64.Type}}),
    op= [#"+"=Value.Add,#"*"=Value.Multiply,#"/"=Value.Divide,#"-"=Value.Subtract],
    #"Aggiunta colonna personalizzata" = Table.AddColumn(mt, "map", each  let r= Record.FieldOrDefault(op,[Operator],[A]/[B]+1)(mt[Balance Sum]{[A]-1},mt[Balance Sum]{[B]-1}), R=try r in if R[HasError] then R[Error][Detail][Value]??[Balance Sum]  else r)
in
    #"Aggiunta colonna personalizzata"
Anonymous
Not applicable

or

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZBBa8MwDIX/ishxa1kTN013XNkKu2yDdLuEHoytNgYnKo6b0X8/OWk3FoOFJXif35OrKkmTWbIjLy2U0mLH03uPTlrLXbFeLPi6nv2sSrJIvSF9gbKmE/dZPtWLSP/hcP6M1rDJJSCrKbKMkDf8hi+sjRrHrJgiK5FzO8jhhA6k7k1HDra7l0lCPg9cYWkxLiQeQ8bPtiOroaazCxYlut4oHIF58ORaDkCR5r8BX1tFTVA96ca0pvNOekPtiN1fsZtPkQ0+qpbuiBqkBzwcUHn+CWAO46R3A30z5hdE7Pwv6ZZcc7Yy/XPe/wA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"KPI Number" = _t, #"KPI Name" = _t, Department = _t, #"Balance Sum" = _t, Operator = _t, A = _t, B = _t]),
    mt = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}, {"Balance Sum", Int64.Type}}),
    op= [#"+"=Value.Add,#"*"=Value.Multiply,#"/"=Value.Divide,#"-"=Value.Subtract,Formula1=(x,y)=>x/y+1,#""=(x,y)=>null],
    #"Aggiunta colonna personalizzata" = Table.AddColumn(mt, "map", each  Record.Field(op,[Operator])(mt[Balance Sum]{[A]-1??0},mt[Balance Sum]{[B]-1??0}))
in
    #"Aggiunta colonna personalizzata"

Hi Rocco,

 

The new map column appears to be correct for the KPI rows 635, 2393, 715 & 2372.  It Doesn't account for the Formula1 Operator e.g. (A + B) / B.  There are a number of rows in my actual dataset that have specific formulas rather than using a single operator like +-/*.

Anonymous
Not applicable

Ok. Try this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZBBa8MwDIX/ishxa1kTN013XNkKu2yDdLuEHoytNgYnKo6b0X8/OWk3FoOFJXif35OrKkmTWbIjLy2U0mLH03uPTlrLXbFeLPi6nv2sSrJIvSF9gbKmE/dZPtWLSP/hcP6M1rDJJSCrKbKMkDf8hi+sjRrHrJgiK5FzO8jhhA6k7k1HDra7l0lCPg9cYWkxLiQeQ8bPtiOroaazCxYlut4oHIF58ORaDkCR5r8BX1tFTVA96ca0pvNOekPtiN1fsZtPkQ0+qpbuiBqkBzwcUHn+CWAO46R3A30z5hdE7Pwv6ZZcc7Yy/XPe/wA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"KPI Number" = _t, #"KPI Name" = _t, Department = _t, #"Balance Sum" = _t, Operator = _t, A = _t, B = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}}),
    #"Aggiunta colonna personalizzata" = Table.AddColumn(#"Modificato tipo", "form", each try if [Operator]="Formula1" then Text.From([A]) & "/"& Text.From([B])&"+1" else #"Modificato tipo"[Balance Sum]{[A]-1} & [Operator] & #"Modificato tipo"[Balance Sum]{[B]-1} otherwise [Balance Sum]),
    #"Aggiunta colonna personalizzata1" = Table.AddColumn(#"Aggiunta colonna personalizzata", "expr", each Expression.Evaluate([form]))
in
    #"Aggiunta colonna personalizzata1"

 

 

This is a solution that fits the example you have proposed. I'm not sure it will work in more general cases. If you want a more robust solution, you should provide more comprehensive examples or better explain the structure of your data and the logic behind it.

Perfect! I really appreciate your help.

Anonymous
Not applicable

I'm pretty sure that's not exactly what you want. But just to start ...

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZBBa8MwDIX/ishxa1kTN013XNkKu2yDdLuEHoytNgYnKo6b0X8/OWk3FoOFJXif35OrKkmTWbIjLy2U0mLH03uPTlrLXbFeLPi6nv2sSrJIvSF9gbKmE/dZPtWLSP/hcP6M1rDJJSCrKbKMkDf8hi+sjRrHrJgiK5FzO8jhhA6k7k1HDra7l0lCPg9cYWkxLiQeQ8bPtiOroaazCxYlut4oHIF58ORaDkCR5r8BX1tFTVA96ca0pvNOekPtiN1fsZtPkQ0+qpbuiBqkBzwcUHn+CWAO46R3A30z5hdE7Pwv6ZZcc7Yy/XPe/wA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"KPI Number" = _t, #"KPI Name" = _t, Department = _t, #"Balance Sum" = _t, Operator = _t, A = _t, B = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}}),
    #"Aggiunta colonna personalizzata" = Table.AddColumn(#"Modificato tipo", "form", each try if [Operator]="Formula1" then #"Modificato tipo"[Balance Sum]{[A]-1} & "/" & #"Modificato tipo"[Balance Sum]{[B]-1}&"+1" else #"Modificato tipo"[Balance Sum]{[A]-1} & [Operator] & #"Modificato tipo"[Balance Sum]{[B]-1} otherwise [Balance Sum]),
    #"Aggiunta colonna personalizzata1" = Table.AddColumn(#"Aggiunta colonna personalizzata", "expr", each Expression.Evaluate([form]))
in
    #"Aggiunta colonna personalizzata1"

 

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.

Top Solution Authors
Top Kudoed Authors