Forum Discussion
Add Custom column based on string in another column (if device = "iPhone" or "iPad" then do this)?
- 7 years ago
astrbac If you just looking for the new column "Correct Total Sales" logic, then please add "Custom Column" in Power Query as below:
if List.Contains({"iPhone","iPad","iPod"},[Platform]) then ([MobileSales]/2) + [WebSales] else [MobileSales] + [WebSales]
Try:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcgtS0lHKDMjIz0sFMgyNjYyBlIW5gaVSrE60kmcIWDYxBSRnZGJsAqTNzUzNwZJgrYl5KUX5mSB5I7CskbEpEJkidCMUGBpBTAcqBCqNjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Market = _t, Platform = _t, #"Web sales $" = _t, #"Mobile sales $" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Market", type text}, {"Platform", type text}, {"Web sales $", Int64.Type}, {"Mobile sales $", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "New Mobile Sales", each if List.Contains( {"iPod", "iPad", "iPhone" }, [Platform] ) then [#"Mobile sales $"] / 2 else [#"Mobile sales $"], type number),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Mobile sales $"}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Market", "Platform"}, "Category", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Market"}, {{"TotalSales", each List.Sum([Value]), type number}})
in
#"Grouped Rows"- astrbac7 years agoFrequent Visitor
A-HA! :) so that is it... this works sort of like recording a Macro and then just the "raw" M language is copied and shared.
In that case, how do I use this? This is what I did:
- I opened my document, clicked the Query name in the right side panel;
- I chose Query > Edit;
- In the Power query Editor I selected the "Add custom column";
- Pasted Greg_Deckler code into it and added the column;
This did produce the small table with FR and IT results. However, in mz initial post I simpliffied the problem mz creating this simple dataset. The real data that I have is some 20 fields and 15.000 records. Greg's code works for the example but not for my real life case.
Generically, this is what I need:
- create custom field (called "Corrected Total sales");
- by adding two already existing fields ("Website conversion value" and "Mobile app conversions value");
- based on the values in a third field (if "Impression device" equals {iPhone, iPad, iPod}... then divide /2, otherwise add normally);
This is what was already in there in the steps I managed to do myself:
let Source = Csv.Document(File.Contents("C:\Users\Ed Chigliak\Google Drive\XXXXX\XXXXXXX\reports\raw-data\fb\XXXXXXXXXX-FR-IT-ES-XXXXXXX-Ad-sets-1-May-201814-October-2018.csv"),[Delimiter=",", Columns=28, Encoding=1252, QuoteStyle=QuoteStyle.None]), #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Reporting starts", type date}, {"Reporting ends", type date}, {"Ad set name", type text}, {"Platform", type text}, {"Placement", type text}, {"Device platform", type text}, {"Impression Device", type text}, {"Campaign name", type text}, {"Delivery", type text}, {"Date created", type date}, {"Budget", Int64.Type}, {"Budget Type", type text}, {"Amount spent (GBP)", type number}, {"Website conversion value", type text}, {"Mobile app purchases conversion value", type number}, {"Website conversions", Int64.Type}, {"Mobile app purchases", Int64.Type}, {"Website purchase ROAS (return on advertising spend)", type text}, {"Mobile app purchase ROAS (return on advertising spend)", type number}, {"Results", Int64.Type}, {"Result indicator", type text}, {"Cost per results", type number}, {"Reach", Int64.Type}, {"Impressions", Int64.Type}, {"Link clicks", Int64.Type}, {"Landing page views", Int64.Type}, {"Ends", type text}, {"Starts", type date}}) in #"Changed Type"Cheers!
- PattemManohar7 years agoCommunity Champion
astrbac If you just looking for the new column "Correct Total Sales" logic, then please add "Custom Column" in Power Query as below:
if List.Contains({"iPhone","iPad","iPod"},[Platform]) then ([MobileSales]/2) + [WebSales] else [MobileSales] + [WebSales]- astrbac7 years agoFrequent Visitor
PattemManohar Greg_Deckler LivioLanzo
Pattem, this worked! :) Now, I don't know whose solution I should accept, since all of you helped out but I didn't explain well in the beginning what I needed.
Any advice here?