PowerBI - 两行与多 table 过滤器相结合的差异
PowerBI - Difference between two rows combined with multi-table filters
我是 powerbi DAX 的新手,正在努力计算两条线之间的差异。
该数据集描述了一个公交网络,由三个 table 组成。示例如下:
表 1
Transport_type, Line_number
Bus, 101
Bus, 102
表 2
Line_number, Trip, Stop_name, Stop_sequence
101, monday-101-1, stop-101-a, 1
101, monday-101-1, stop-101-b, 2
101, monday-101-1, stop-101-c, 3
101, monday-101-2, stop-101-a, 1
101, monday-101-2, stop-101-c, 3
101, monday-101-2, stop-101-b, 2
102, monday-102-1, stop-102-c, 3
102, monday-102-1, stop-102-b, 2
102, monday-102-1, stop-102-a, 1
102, monday-102-2, stop-102-a, 1
102, monday-102-2, stop-102-b, 2
102, monday-102-2, stop-102-c, 3
表 3
Stop_name, Altitude
stop-101-a, 540
stop-101-b, 560
stop-101-c, 590
stop-102-a, 230
stop-102-b, 210
stop-102-c, 170
table 之间的关系已在模型选项卡中设置;在报告选项卡中,我创建了一个 table 并通过拖放添加了所有参数。
选择 Table3 后,我创建了一个新的计算列并拼命尝试计算行程中每个站点之间的高度差。
预期结果示例
Transport_type, Line_number, Trip, Stop_name, Stop_sequence, Altitude difference
Bus, 101, monday-101-1, stop-101-a, 1, 0
Bus, 101, monday-101-1, stop-101-b, 2, 20 (=560-540)
Bus, 101, monday-101-1, stop-101-c, 3, 30 (=590-560)
Bus, 101, monday-101-2, stop-101-a, 1, 0
Bus, 101, monday-101-2, stop-101-c, 3, 30 (=590-560)
Bus, 101, monday-101-2, stop-101-b, 2, 20 (=560-540)
Bus, 102, monday-102-1, stop-102-c, 3, -40 (=170-210)
Bus, 102, monday-102-1, stop-102-b, 2, -20 (=210-230)
Bus, 102, monday-102-1, stop-102-a, 1, 0
Bus, 102, monday-102-2, stop-102-a, 1, 0
Bus, 102, monday-102-2, stop-102-b, 2, -20 (=210-230)
Bus, 102, monday-102-2, stop-102-c, 3, -40 (=170-210)
有没有人知道如何组合来自不同 table 的过滤器然后减去两行?
感谢您的帮助:)
您可以在 power query 中进行一些转换以获得所需的输出。我已经考虑了 table2 和 Table3 。下面是 Advanced Query for Table2-
重要提示: 停止序列在这里发挥了主要作用,找到了sopt_name的开始和结束。
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WciotVtJRMjQwBJK5+XkpiZW6QI4uiFtckl8A5iSCVCjF6hCnOgnIMSJadTKQY4xPtRFJLjEi32xMdxshqzZCdrcRFrPxqSbNbExfoqk2Il81QZcYYfFlLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Transport_type = _t, Line_number = _t, Trip = _t, Stop_name = _t, Stop_sequence = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Transport_type", type text}, {"Line_number", Int64.Type}, {"Trip", type text}, {"Stop_name", type text}, {"Stop_sequence", Int64.Type}}),
//-- My transformation starts from here
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Line_number", Order.Ascending}, {"Trip", Order.Ascending}, {"Stop_sequence", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each [Stop_sequence] - 1),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Line_number", "Trip", "Custom"}, #"Added Custom", {"Line_number", "Trip", "Stop_sequence"}, "Added Custom", JoinKind.LeftOuter),
#"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"Stop_name"}, {"Added Custom.Stop_name"}),
#"Renamed Columns" = Table.RenameColumns(#"Expanded Added Custom",{{"Added Custom.Stop_name", "stop_name_start"}}),
#"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Transport_type", "Line_number", "Trip", "stop_name_start", "Stop_name", "Stop_sequence", "Custom"}),
#"Merged Queries1" = Table.NestedJoin(#"Reordered Columns", {"stop_name_start"}, table3, {"Stop_name"}, "table3", JoinKind.LeftOuter),
#"Expanded table3" = Table.ExpandTableColumn(#"Merged Queries1", "table3", {"Altitude"}, {"table3.Altitude"}),
#"Merged Queries2" = Table.NestedJoin(#"Expanded table3", {"Stop_name"}, table3, {"Stop_name"}, "table3", JoinKind.LeftOuter),
#"Expanded table1" = Table.ExpandTableColumn(#"Merged Queries2", "table3", {"Altitude"}, {"table3.Altitude.1"}),
#"Renamed Columns1" = Table.RenameColumns(#"Expanded table1",{{"table3.Altitude", "start_altitude"}, {"table3.Altitude.1", "stop_altitude"}}),
#"Added Custom1" = Table.AddColumn(#"Renamed Columns1", "Custom.1", each [stop_altitude] - [start_altitude]),
#"Renamed Columns2" = Table.RenameColumns(#"Added Custom1",{{"Custom.1", "distance"}})
in
#"Renamed Columns2"
这里是最终输出-
非常感谢您的帮助!
我正在尝试复制您的解决方案,但在第一个合并查询时出错:
let
Source = Csv.Document(File.Contents("C:\Users\l.charles-antoine\Desktop\Test BI\gtfs_fp2020_2020-08-26\test alt-dist\stop_tumes.csv"),[Delimiter=";", Columns=4, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Line_number", Int64.Type}, {" Trip", type text}, {" Stop_name", type text}, {" Stop_sequence", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Line_number", Order.Ascending}, {" Trip", Order.Ascending}, {" Stop_sequence", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each ([#" Stop_sequence"])-1),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Line_number", "Trip", "Custom"}, #"Added Custom", {"Line_number", "Trip", "Stop_sequence"}, "Added Custom", JoinKind.LeftOuter)
in
#"Merged Queries"
错误是:Expression.Error:找不到 table 的列 'Trip'。
细节:
旅行
我是 powerbi DAX 的新手,正在努力计算两条线之间的差异。
该数据集描述了一个公交网络,由三个 table 组成。示例如下:
表 1
Transport_type, Line_number
Bus, 101
Bus, 102
表 2
Line_number, Trip, Stop_name, Stop_sequence
101, monday-101-1, stop-101-a, 1
101, monday-101-1, stop-101-b, 2
101, monday-101-1, stop-101-c, 3
101, monday-101-2, stop-101-a, 1
101, monday-101-2, stop-101-c, 3
101, monday-101-2, stop-101-b, 2
102, monday-102-1, stop-102-c, 3
102, monday-102-1, stop-102-b, 2
102, monday-102-1, stop-102-a, 1
102, monday-102-2, stop-102-a, 1
102, monday-102-2, stop-102-b, 2
102, monday-102-2, stop-102-c, 3
表 3
Stop_name, Altitude
stop-101-a, 540
stop-101-b, 560
stop-101-c, 590
stop-102-a, 230
stop-102-b, 210
stop-102-c, 170
table 之间的关系已在模型选项卡中设置;在报告选项卡中,我创建了一个 table 并通过拖放添加了所有参数。
选择 Table3 后,我创建了一个新的计算列并拼命尝试计算行程中每个站点之间的高度差。
预期结果示例
Transport_type, Line_number, Trip, Stop_name, Stop_sequence, Altitude difference
Bus, 101, monday-101-1, stop-101-a, 1, 0
Bus, 101, monday-101-1, stop-101-b, 2, 20 (=560-540)
Bus, 101, monday-101-1, stop-101-c, 3, 30 (=590-560)
Bus, 101, monday-101-2, stop-101-a, 1, 0
Bus, 101, monday-101-2, stop-101-c, 3, 30 (=590-560)
Bus, 101, monday-101-2, stop-101-b, 2, 20 (=560-540)
Bus, 102, monday-102-1, stop-102-c, 3, -40 (=170-210)
Bus, 102, monday-102-1, stop-102-b, 2, -20 (=210-230)
Bus, 102, monday-102-1, stop-102-a, 1, 0
Bus, 102, monday-102-2, stop-102-a, 1, 0
Bus, 102, monday-102-2, stop-102-b, 2, -20 (=210-230)
Bus, 102, monday-102-2, stop-102-c, 3, -40 (=170-210)
有没有人知道如何组合来自不同 table 的过滤器然后减去两行?
感谢您的帮助:)
您可以在 power query 中进行一些转换以获得所需的输出。我已经考虑了 table2 和 Table3 。下面是 Advanced Query for Table2-
重要提示: 停止序列在这里发挥了主要作用,找到了sopt_name的开始和结束。
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WciotVtJRMjQwBJK5+XkpiZW6QI4uiFtckl8A5iSCVCjF6hCnOgnIMSJadTKQY4xPtRFJLjEi32xMdxshqzZCdrcRFrPxqSbNbExfoqk2Il81QZcYYfFlLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Transport_type = _t, Line_number = _t, Trip = _t, Stop_name = _t, Stop_sequence = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Transport_type", type text}, {"Line_number", Int64.Type}, {"Trip", type text}, {"Stop_name", type text}, {"Stop_sequence", Int64.Type}}),
//-- My transformation starts from here
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Line_number", Order.Ascending}, {"Trip", Order.Ascending}, {"Stop_sequence", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each [Stop_sequence] - 1),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Line_number", "Trip", "Custom"}, #"Added Custom", {"Line_number", "Trip", "Stop_sequence"}, "Added Custom", JoinKind.LeftOuter),
#"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"Stop_name"}, {"Added Custom.Stop_name"}),
#"Renamed Columns" = Table.RenameColumns(#"Expanded Added Custom",{{"Added Custom.Stop_name", "stop_name_start"}}),
#"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Transport_type", "Line_number", "Trip", "stop_name_start", "Stop_name", "Stop_sequence", "Custom"}),
#"Merged Queries1" = Table.NestedJoin(#"Reordered Columns", {"stop_name_start"}, table3, {"Stop_name"}, "table3", JoinKind.LeftOuter),
#"Expanded table3" = Table.ExpandTableColumn(#"Merged Queries1", "table3", {"Altitude"}, {"table3.Altitude"}),
#"Merged Queries2" = Table.NestedJoin(#"Expanded table3", {"Stop_name"}, table3, {"Stop_name"}, "table3", JoinKind.LeftOuter),
#"Expanded table1" = Table.ExpandTableColumn(#"Merged Queries2", "table3", {"Altitude"}, {"table3.Altitude.1"}),
#"Renamed Columns1" = Table.RenameColumns(#"Expanded table1",{{"table3.Altitude", "start_altitude"}, {"table3.Altitude.1", "stop_altitude"}}),
#"Added Custom1" = Table.AddColumn(#"Renamed Columns1", "Custom.1", each [stop_altitude] - [start_altitude]),
#"Renamed Columns2" = Table.RenameColumns(#"Added Custom1",{{"Custom.1", "distance"}})
in
#"Renamed Columns2"
这里是最终输出-
非常感谢您的帮助!
我正在尝试复制您的解决方案,但在第一个合并查询时出错:
let
Source = Csv.Document(File.Contents("C:\Users\l.charles-antoine\Desktop\Test BI\gtfs_fp2020_2020-08-26\test alt-dist\stop_tumes.csv"),[Delimiter=";", Columns=4, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Line_number", Int64.Type}, {" Trip", type text}, {" Stop_name", type text}, {" Stop_sequence", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Line_number", Order.Ascending}, {" Trip", Order.Ascending}, {" Stop_sequence", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each ([#" Stop_sequence"])-1),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Line_number", "Trip", "Custom"}, #"Added Custom", {"Line_number", "Trip", "Stop_sequence"}, "Added Custom", JoinKind.LeftOuter)
in
#"Merged Queries"
错误是:Expression.Error:找不到 table 的列 'Trip'。 细节: 旅行