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
I'm sorry I don't think I can follow your solution. I can create the table with names and each name having all the corresponding dates. However after I merge with the unpivoted data I don't seem to get it right because whatever I do I end up with the same situation as before, I will either have rows with 0's despite there already being data or not every name will have a row for each date in the dataset.
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
- Zepox4 years agoFrequent Visitor
Amazing, thank you for the detailed response. It fully works as intended now.
Edit: Loading time with your solution is also only a third from what it was with mine, thanks again.