Sort Power Query Graph Left to Right based on last row

JohnnyPicnic

New Member
Joined
Feb 8, 2021
Messages
17
Office Version
  1. 2019
Platform
  1. Windows
Hi all, I'm looking to sort a graph based on the values in last row with smallest or blank values to the left and largest to the right. I'm using power query and a pivot table to pull the data from csv files.
I can't seem to find anything in the query editor and "Sort left to right" is greyed out.


graph1.png
 

Attachments

  • graph2.png
    graph2.png
    96.8 KB · Views: 4

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I sort figured out my own solution but it's only working for one graph and not another.
After the table is pivoted and blanks are populated with nulls. Replaced all nulls with "0". Unpivoted the table. Sorted the Values column. Re-pivoted the table.
One graph gives me the expected results but the other seems random.
Working graph and code:

graph3.png

Code:
let
    Source = Folder.Files("Z:\Isco_Battery_Graphs"),
    #"Filtered Rows" = Table.SelectRows(Source, each [Extension] = ".csv"),
    #"Filtered Hidden Files1" = Table.SelectRows(#"Filtered Rows", each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File from Test", each #"Transform File from Test"([Content])),
    #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File from Test"}),
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File from Test", Table.ColumnNames(#"Transform File from Test"(#"Sample File"))),
    #"Filtered Rows3" = Table.SelectRows(#"Expanded Table Column1", each Text.Contains([Column1], "AM")),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows3", each Text.Contains([Source.Name], "LA")),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows1",{{"Column2", type number}, {"Column1", type datetime}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Column2", Order.Descending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Source.Name]), "Source.Name", "Column2"),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"Column1", "Date"}}),
    #"Replaced Value" = Table.ReplaceValue(#"Renamed Columns",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Renamed Columns")),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Replaced Value", {"Date"}, "Attribute", "Value"),
    #"Sorted Rows1" = Table.Sort(#"Unpivoted Other Columns",{{"Value", Order.Ascending}}),
    #"Pivoted Column1" = Table.Pivot(#"Sorted Rows1", List.Distinct(#"Sorted Rows1"[Attribute]), "Attribute", "Value", List.Sum),
    #"Replaced Value1" = Table.ReplaceValue(#"Pivoted Column1",0,null,Replacer.ReplaceValue,Table.ColumnNames(#"Pivoted Column1")),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value1",{{"Date", type date}})
in
    #"Changed Type1"

Non-Working graph and code

graph4.png


Code:
let
    Source = Folder.Files("Z:\campbell_diagnostics"),
    #"Filtered Rows1" = Table.SelectRows(Source, each Text.Contains([Name], "Bat")),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows1", "Custom", each [Content]),
    #"Filtered Hidden Files1" = Table.SelectRows(#"Added Custom", each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File from Diagnostics", each #"Transform File from Diagnostics"([Custom])),
    #"Renamed Columns" = Table.RenameColumns(#"Invoke Custom Function1",{{"Name", "Source.Name"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Content", "Extension", "Date accessed", "Date modified", "Date created", "Attributes", "Folder Path", "Custom"}),
    #"Replaced Value" = Table.ReplaceValue(#"Removed Columns","_Battery_Level.dat","          ",Replacer.ReplaceText,{"Source.Name"}),
    #"Expanded Transform File from Diagnostics" = Table.ExpandTableColumn(#"Replaced Value", "Transform File from Diagnostics", {"Column1", "Column3"}, {"Transform File from Diagnostics.Column1", "Transform File from Diagnostics.Column3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Transform File from Diagnostics",{{"Transform File from Diagnostics.Column3", type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type1", each Text.Contains([Transform File from Diagnostics.Column1], ":00")),
    #"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Source.Name]), "Source.Name", "Transform File from Diagnostics.Column3"),
    #"Renamed Columns1" = Table.RenameColumns(#"Pivoted Column",{{"Transform File from Diagnostics.Column1", "Date"}}),
    #"Replaced Value1" = Table.ReplaceValue(#"Renamed Columns1",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Renamed Columns1")),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","00:00:00","",Replacer.ReplaceText,{"Date"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value2",{{"Date", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "Attribute", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"Value", Order.Ascending}}),
    #"Pivoted Column1" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute]), "Attribute", "Value"),
    #"Replaced Value3" = Table.ReplaceValue(#"Pivoted Column1",0,null,Replacer.ReplaceValue,Table.ColumnNames(#"Pivoted Column1"))

in
    #"Replaced Value3"
 
Upvote 0
Well I managed to get it sorted. Not totally sure if it's 100% correct but it seems to work.
Ended up needing to sort the date in an unpivoted table so the newest is at the top and then sort the values.
Hopefully this will help someone else as well...

graph5.png



Code:
let
    Source = Folder.Files("Z:\campbell_diagnostics"),
    #"Filtered Rows1" = Table.SelectRows(Source, each Text.Contains([Name], "Bat")),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows1", "Custom", each [Content]),
    #"Filtered Hidden Files1" = Table.SelectRows(#"Added Custom", each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File from Diagnostics", each #"Transform File from Diagnostics"([Custom])),
    #"Removed Columns" = Table.RemoveColumns(#"Invoke Custom Function1",{"Content", "Extension", "Date accessed", "Date modified", "Date created", "Attributes", "Folder Path", "Custom"}),
    #"Expanded Transform File from Diagnostics" = Table.ExpandTableColumn(#"Removed Columns", "Transform File from Diagnostics", {"Column1", "Column3"}, {"Transform File from Diagnostics.Column1", "Transform File from Diagnostics.Column3"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Transform File from Diagnostics", each Text.Contains([Transform File from Diagnostics.Column1], ":00")),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"Transform File from Diagnostics.Column3", type number}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Name]), "Name", "Transform File from Diagnostics.Column3"),
    #"Renamed Columns1" = Table.RenameColumns(#"Pivoted Column",{{"Transform File from Diagnostics.Column1", "Date"}}),
    #"Replaced Value2" = Table.ReplaceValue(#"Renamed Columns1","00:00:00","",Replacer.ReplaceText,{"Date"}),
    #"Replaced Value" = Table.ReplaceValue(#"Replaced Value2",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Replaced Value2")),
    #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Date", type date}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "Attribute", "Value"),
    #"Replaced Value3" = Table.ReplaceValue(#"Unpivoted Columns","_Battery_Level.dat","     ",Replacer.ReplaceText,{"Attribute"}),
    #"Sorted Rows" = Table.Sort(#"Replaced Value3",{{"Date", Order.Descending}, {"Value", Order.Ascending}}),
    #"Pivoted Column1" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute]), "Attribute", "Value", List.Sum),
    #"Filtered Rows2" = Table.SelectRows(#"Pivoted Column1", let latest = List.Max(#"Pivoted Column1"[Date]) in each Date.IsInPreviousNDays([Date], 30) or [Date] = latest),
    #"Replaced Value1" = Table.ReplaceValue(#"Filtered Rows2",0,null,Replacer.ReplaceValue,Table.ColumnNames(#"Filtered Rows2"))

in
    #"Replaced Value1"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,208
Messages
6,123,644
Members
449,111
Latest member
ghennedy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top