Forum Discussion
Change value if column already has a value on given date.
- 4 years ago
Create query with name Users
let Source = {"Peter", "Pan", "Jane"}, #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Added Custom" = Table.AddColumn(#"Converted to Table", "Dates", each List.Numbers(44721,20)), #"Expanded Dates" = Table.ExpandListColumn(#"Added Custom", "Dates"), #"Changed Type" = Table.TransformColumnTypes(#"Expanded Dates",{{"Dates", type date}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Name"}}), #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Name", type text}}) in #"Changed Type1"Create secon query with any name
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"FullDateList"}, "Attribute", "Value"), #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Name"}}), Custom1 = Users, #"Merged Queries" = Table.NestedJoin(Custom1, {"Name", "Dates"}, Table1, {"Attribute", "FullDateList"}, "Table1", JoinKind.LeftOuter), #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Value"}, {"Value"}), #"Sorted Rows" = Table.Sort(#"Expanded Table1",{{"Dates", Order.Ascending}, {"Name", Order.Ascending}}), #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Dates", "Name", "Value"}) in #"Reordered Columns"Second query merges query Users and shows data for every name and every date.
Second solution from my first post would be something like this
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"FullDateList", type date}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"FullDateList"}, {{"Peter", each List.RemoveItems([Peter],{0,null}), type any}, {"Pan", each List.RemoveItems([Pan],{0,null}), type any}, {"Jane", each List.RemoveItems([Jane],{0,null}), type any}}), #"Expanded Jane" = Table.ExpandListColumn(#"Grouped Rows", "Jane"), #"Expanded Pan" = Table.ExpandListColumn(#"Expanded Jane", "Pan"), #"Expanded Peter" = Table.ExpandListColumn(#"Expanded Pan", "Peter"), #"Replaced Value" = Table.ReplaceValue(#"Expanded Peter",null,"0",Replacer.ReplaceValue,{"Peter", "Pan", "Jane"}), #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Replaced Value", {"Peter", "Pan", "Jane"}, "Attribute", "Value") in #"Unpivoted Only Selected Columns"I woul got with the solution with two queries because you can find what is minimum and maximum dates in your input data and find unique names in the source data and automate fully the query preparation of basic data in query Users.
I hope it will help you create your solution.
Artur
Hi Zepox ,
Please try this :
In power query, select columns Peter and Pan in your table, and click on Replace Values.
Replace 0 with null and unpivot columns Peter, Pan and Jane.
The extra rows for Peter and Pan will be removed and the 0's for Jane will be kept.
Kind regards,
Rohit
Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! :}
- Zepox4 years agoFrequent Visitor
Hello,
This wouldn't really work as days where Peter or Pan don't have data, would also get removed. These should still be a datapoint just like Jane where it will be just the date, name and thee 0 for the other column(s). Also this way Jane would have empty rows for dates that already have data.
Regards,