Forum Discussion

Zepox's avatar
Zepox
Frequent Visitor
4 years ago
Solved

Change value if column already has a value on given date.

Hello all, I have a table which I pivoted to fill a name column with data for each date. The problem that I now have is empty rows of data on dates that already had data in the dataset. In image 1 b...
  • artpil's avatar
    artpil
    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