[英]How to custom order week year in power bi?
I have issue in custom ordering data.我有自定义订购数据的问题。 Here is example
这是示例
I would like to be sorted first by year and than by week我想先按年排序,然后按周排序
I want 1 2022 to be at the and of the line.我希望 1 2022 位于该行的 and 处。
I have tried adding few things.我尝试添加一些东西。 Is there a way to sort this out by adding new table with weekYear column and some sortOrder column which I have to populate with custom values as well?
有没有办法通过添加带有 weekYear 列的新表和一些我必须用自定义值填充的 sortOrder 列来解决这个问题?
I prefer solution to be dynamic..我更喜欢动态的解决方案..
If you have any solution please advice!如果有解决办法请指教!
I suggest我建议
I did this as a custom function:我这样做是作为自定义 function:
(tbl as table, optional colsToSort as list) =>
let
colHdrs = if colsToSort=null then Table.ColumnNames(tbl) else colsToSort,
hdrCol = Table.FromList(colHdrs,Splitter.SplitTextByDelimiter(" ")),
//type as number for sorting
typeAsNumber = Table.TransformColumnTypes(hdrCol,{{"Column1", Int64.Type},{"Column2",Int64.Type}}),
sorted = Table.Sort(typeAsNumber,{{"Column2", Order.Ascending}, {"Column1", Order.Ascending}}),
//type as text to combine
typeAsText = Table.TransformColumnTypes(sorted,{{"Column1", Text.Type}, {"Column2", Text.Type}}),
combine = Table.CombineColumns(typeAsText,{"Column1","Column2"},Combiner.CombineTextByDelimiter(" "),"orderHeaders"),
//re-Order the column headers in the original table
newOrder = Table.ReorderColumns(tbl,combine[orderHeaders])
in
newOrder
And you could call it from your original query as:您可以从原始查询中将其称为:
let
Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"41 2021", Int64.Type}, {"42 2021", Int64.Type}, {"43 2021", Int64.Type},
{"46 2021", Int64.Type}, {"1 2022", Int64.Type}, {"48 2021", Int64.Type},
{"50 2021", Int64.Type}, {"49 2021", Int64.Type}, {"51 2021", Int64.Type},
{"52 2021", Int64.Type}}),
//custom function here
sortHdrs = fnSortHeaders(#"Changed Type")
in
sortHdrs
If ALL of your columns are in the format of weeknumber year
so that you want to sort ALL of them, it may be simpler to do this without a separate function.如果您的所有列都采用
weeknumber year
的格式,以便您想要对所有列进行排序,那么在没有单独的 function 的情况下执行此操作可能会更简单。 The advantage of the separate function is that you can more easily remove the column names you don't want to include in the sort单独的 function 的优点是您可以更轻松地删除您不想包含在排序中的列名
Here is code that will sort the column headers with no need of a separate function.这是无需单独的 function 即可对列标题进行排序的代码。 It uses the same algorithm as above
它使用与上面相同的算法
let
Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
#"Changed Type2" = Table.TransformColumnTypes(Source,{
{"41 2021", Int64.Type}, {"42 2021", Int64.Type}, {"43 2021", Int64.Type},
{"46 2021", Int64.Type}, {"1 2022", Int64.Type}, {"48 2021", Int64.Type},
{"50 2021", Int64.Type}, {"49 2021", Int64.Type}, {"51 2021", Int64.Type},
{"52 2021", Int64.Type}}),
//create header list
//this assumes ALL column headers are to be sorted, but you can make partial lists for the column sorting
hdrs = List.Combine(List.Transform(Table.ColumnNames(#"Changed Type2"), each Text.Split(_," "))),
//split into month and year and put into a table
monthYear = Table.FromColumns({List.Alternate(hdrs,1,1,1),List.Alternate(hdrs,1,1,0)},{"Month","Year"}),
//change data type to number and sort by Year and then Month
#"Changed Type" = Table.TransformColumnTypes(monthYear,{{"Month", Int64.Type}, {"Year", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Year", Order.Ascending}, {"Month", Order.Ascending}}),
//change data type to text and join with space delimiter
#"Changed Type1" = Table.TransformColumnTypes(#"Sorted Rows",{{"Month", type text}, {"Year", type text}}),
sortedHeaders = Table.CombineColumns(#"Changed Type1",{"Month", "Year"},
Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"newOrder")[newOrder],
//reOrder the original table
sortedTableColumns= Table.ReorderColumns(#"Changed Type2",sortedHeaders)
in
sortedTableColumns
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.