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
sankhadeep22
New Member

How to add values of a column based on another column

Hi ALL,

StateFuel TypeCount
WBDual100
WBElectricity35
WBGas20
BIHDual115
BIHElectricity25
BIHGas10
TNGas40

I am Novice at Power BI. I have the above table. My requirement is as 

to add "dual" fuel values to both electricity and gas where there are dual connections else keep as is. I would only consider "gas" and "electricity" in the table format, which I would like to change into chart later. Below is the required table output

 

StateFuelCount
WBElectricity135
WBGas120
BIHElectricity140
BIHGas125
TNGas40

 

Please help

 

Thanks

S

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

Hi @sankhadeep22 ,,

 

Create calculated column.

Column = 
var dual = CALCULATE(SUM('Table'[Count]),FILTER('Table','Table'[State]=EARLIER('Table'[State])&&'Table'[Fuel Type]="Dual"))
return
IF('Table'[Fuel Type]="Dual",'Table'[Count],'Table'[Count]+dual)

 1.PNG

Create calculated table.

Table 2 = FILTER('Table','Table'[Fuel Type]<>"Dual")

2.PNG

 

Best Regards,

Jay

Community Support Team _ Jay Wang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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

View solution in original post

5 REPLIES 5
v-jayw-msft
Community Support
Community Support

Hi @sankhadeep22 ,

 

Have your problem be solved? Please consider accept the answer as a solution if it worked.

 

Best Regards,

Jay

Community Support Team _ Jay Wang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
v-jayw-msft
Community Support
Community Support

Hi @sankhadeep22 ,,

 

Create calculated column.

Column = 
var dual = CALCULATE(SUM('Table'[Count]),FILTER('Table','Table'[State]=EARLIER('Table'[State])&&'Table'[Fuel Type]="Dual"))
return
IF('Table'[Fuel Type]="Dual",'Table'[Count],'Table'[Count]+dual)

 1.PNG

Create calculated table.

Table 2 = FILTER('Table','Table'[Fuel Type]<>"Dual")

2.PNG

 

Best Regards,

Jay

Community Support Team _ Jay Wang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
Mariusz
Community Champion
Community Champion

Hi @sankhadeep22 

 

Please see the attached file with a solution using Power Query

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Mariusz
Community Champion
Community Champion

Hi @sankhadeep22 

 

Sure, just paste this into QE Blank Query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCndS0lFyKU3MAVKGBgZKsTpQMdec1OSSoszkzJJKIM/YFCHjnlgMJI0gap08PZAMMDRFEkQ1wQhZCmKEIcSIED+4iAlQJBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [State = _t, #"Fuel Type" = _t, Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"State", type text}, {"Fuel Type", type text}, {"Count", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[#"Fuel Type"]), "Fuel Type", "Count", List.Sum),
    #"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,{"Dual", "Electricity"}),
    #"Added Custom" = Table.AddColumn( #"Replaced Value", "Electricity ", each [Electricity] + [Dual], Int64.Type ),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Gas ", each [Gas] + [Dual], Int64.Type ),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"State", "Electricity ", "Gas "}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{"Electricity ", "Electricity"}, {"Gas ", "Gas"}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Renamed Columns", {"State"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Columns", each ([Value] <> 0))
in
    #"Filtered Rows"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Hi @Mariusz ,

 

When I open the pbix file , it shows "Unable to open the document - the queries were authored using a newer version of desktop and might not work with your version" .

 

I am using version 2.75 of desktop..

Can you provide me the solution in a reply?

 

 

Thanks ,

S

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.