如何使用第一个和最后一个选项在 Power Bi 中创建新变量?
How to use first and last option to create new variable in Power Bi?
我想根据每个 ID 变量的第一个和最后一个记录创建一个新变量。下面给出了示例参考 table,这将使您更容易理解问题。
Table:-
pkBookingItemID StartTime EndTime BusinessStartTime BusinessEndTime
3417 13:45 14:00 09:00 17:00
3417 13:45 14:00 09:00 17:00
3417 13:45 14:00 09:00 17:00
3417 13:45 14:00 09:00 17:00
3418 10:00 15:00 09:00 16:00
3418 10:00 15:00 09:00 16:00
3418 10:00 15:00 09:00 16:00
我想根据 pkBookingItemID
发生率创建变量 New_BusinessStartTime
和 New_BusinessEndTime
。
期望的输出table:-
pkBookingItemID StartTime EndTime BusinessStartTime BusinessEndTime New_BusinessStartTime New_BusinessEndTime
3417 13:45 14:00 09:00 17:00 13:45 17:00
3417 13:45 14:00 09:00 17:00 09:00 17:00
3417 13:45 14:00 09:00 17:00 09:00 17:00
3417 13:45 14:00 09:00 17:00 09:00 14:00
3418 10:00 15:00 09:00 16:00 10:00 16:00
3418 10:00 15:00 09:00 16:00 09:00 16:00
3418 10:00 15:00 09:00 16:00 09:00 15:00
我必须通过 Power BI 中的 M 查询来实现。到目前为止,我一直在尝试获得所需的 table,但没有取得任何成功。
New_BusinessStartTime =
if [pkBookingItemID] = [pkBookingItemID.First]
then [StartTime] else [BusinessStartTime]
New_BusinessEndTime =
if [pkBookingItemID] = [pkBookingItemID.Last]
then [EndTime] else [BusinessEndTime]
.First 和 .Last 部分并没有像我最初想的那样给出结果。任何人都可以帮助解决这个问题或提供解决这个问题的建议吗?
这里的主要问题是没有什么可以区分行的 'order'。为什么应该将顶部的“3417”行视为第一行而不是其下方的行?我知道这对我们来说似乎很明显……首先列出一行,这是第一行。但这对机器的大脑来说还不够好。它需要数据中的某些内容来帮助它区分行。
所以,我们给它一些东西来使用。首先,我们假设行按照您认为正确的方式排序。然后,我们将添加一个从 1 到任何值的索引号,并确定每个 ItemId 组中最大或最小的索引号。这将告诉我们第一行和最后一行。
然后我们使用您已经计算出的条件逻辑。我们不会针对 pbBookingItemId.First 进行测试,而是检查我们的索引是否与每个组中的 'first' 或 'last' 索引匹配。
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYxNFfSUTI0tjIxBdEmVgYGQNoSQhmag+hYnQFVZwESMIBKmKKqM6OJulgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [pkBookingItemID = _t, StartTime = _t, EndTime = _t, BusinessStartTime = _t, BusinessEndTime = _t]),
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Grouped Rows" = Table.Group(#"Added Index", {"pkBookingItemID"}, {{"AllRows", each _, type table [pkBookingItemID=nullable text, StartTime=nullable text, EndTime=nullable text, BusinessStartTime=nullable text, BusinessEndTime=nullable text, Index=number]}, {"MinIndex", each List.Min([Index]), type number}, {"MaxIndex", each List.Max([Index]), type number}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"pkBookingItemID", "StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "Index"}, {"pkBookingItemID.1", "StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "Index"}),
#"Added Custom" = Table.AddColumn(#"Expanded AllRows", "New_BusinessStartTime", each if [Index] = [MinIndex] then [StartTime] else [BusinessStartTime]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "New_BusinessEndTime", each if [Index] = [MaxIndex] then [EndTime] else [BusinessEndTime])
in
#"Added Custom1"
将上面的代码粘贴到高级查询编辑器中并查看。您将看到分组行和“AddIndexColumn”函数如何协同工作帮助我们选择第一行和最后一行。
然后,我们使用您的条件逻辑来构建 'new' 业务开始或结束时间。
希望对您有所帮助。
您是否在电力查询中使用导入模式?
单击您的 table 并复制您的 table(第一行和最后一行),然后右键单击您的特定列,然后 'remove other column' 复制此 table(第一次和最后一次) .
然后右键单击列旁边的 table 选项,然后选择 'Keep Top rows' 和“保留底行”。
然后使用追加查询将它们合并为一个 table 并将它们用作参数。
您可以在 Table.Group 函数中执行此操作,方法是创建一个新的 table 来获取第一个和最后一个条目的适当值。
例如:
原始数据
M码
...
//Group rows by ID and generate the new start/end columns
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"pkBookingItemID"}, {
{"NewTimes", each Table.FromColumns(
Table.ToColumns(_) &
{List.ReplaceRange([BusinessStartTime],0,1,{[StartTime]{0}}),
List.ReplaceRange([BusinessEndTime],Table.RowCount(_)-1,1,{[EndTime]{Table.RowCount(_)-1}})},
Table.ColumnNames(_) & {"NewBusinessStartTime","NewBusinessEndTime"})
}
}),
#"Expanded NewTimes" = Table.ExpandTableColumn(#"Grouped Rows", "NewTimes", {"StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "NewBusinessStartTime", "NewBusinessEndTime"}, {"StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "NewBusinessStartTime", "NewBusinessEndTime"})
in
#"Expanded NewTimes"
结果
我想根据每个 ID 变量的第一个和最后一个记录创建一个新变量。下面给出了示例参考 table,这将使您更容易理解问题。
Table:-
pkBookingItemID StartTime EndTime BusinessStartTime BusinessEndTime
3417 13:45 14:00 09:00 17:00
3417 13:45 14:00 09:00 17:00
3417 13:45 14:00 09:00 17:00
3417 13:45 14:00 09:00 17:00
3418 10:00 15:00 09:00 16:00
3418 10:00 15:00 09:00 16:00
3418 10:00 15:00 09:00 16:00
我想根据 pkBookingItemID
发生率创建变量 New_BusinessStartTime
和 New_BusinessEndTime
。
期望的输出table:-
pkBookingItemID StartTime EndTime BusinessStartTime BusinessEndTime New_BusinessStartTime New_BusinessEndTime
3417 13:45 14:00 09:00 17:00 13:45 17:00
3417 13:45 14:00 09:00 17:00 09:00 17:00
3417 13:45 14:00 09:00 17:00 09:00 17:00
3417 13:45 14:00 09:00 17:00 09:00 14:00
3418 10:00 15:00 09:00 16:00 10:00 16:00
3418 10:00 15:00 09:00 16:00 09:00 16:00
3418 10:00 15:00 09:00 16:00 09:00 15:00
我必须通过 Power BI 中的 M 查询来实现。到目前为止,我一直在尝试获得所需的 table,但没有取得任何成功。
New_BusinessStartTime =
if [pkBookingItemID] = [pkBookingItemID.First]
then [StartTime] else [BusinessStartTime]
New_BusinessEndTime =
if [pkBookingItemID] = [pkBookingItemID.Last]
then [EndTime] else [BusinessEndTime]
.First 和 .Last 部分并没有像我最初想的那样给出结果。任何人都可以帮助解决这个问题或提供解决这个问题的建议吗?
这里的主要问题是没有什么可以区分行的 'order'。为什么应该将顶部的“3417”行视为第一行而不是其下方的行?我知道这对我们来说似乎很明显……首先列出一行,这是第一行。但这对机器的大脑来说还不够好。它需要数据中的某些内容来帮助它区分行。
所以,我们给它一些东西来使用。首先,我们假设行按照您认为正确的方式排序。然后,我们将添加一个从 1 到任何值的索引号,并确定每个 ItemId 组中最大或最小的索引号。这将告诉我们第一行和最后一行。
然后我们使用您已经计算出的条件逻辑。我们不会针对 pbBookingItemId.First 进行测试,而是检查我们的索引是否与每个组中的 'first' 或 'last' 索引匹配。
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYxNFfSUTI0tjIxBdEmVgYGQNoSQhmag+hYnQFVZwESMIBKmKKqM6OJulgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [pkBookingItemID = _t, StartTime = _t, EndTime = _t, BusinessStartTime = _t, BusinessEndTime = _t]),
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Grouped Rows" = Table.Group(#"Added Index", {"pkBookingItemID"}, {{"AllRows", each _, type table [pkBookingItemID=nullable text, StartTime=nullable text, EndTime=nullable text, BusinessStartTime=nullable text, BusinessEndTime=nullable text, Index=number]}, {"MinIndex", each List.Min([Index]), type number}, {"MaxIndex", each List.Max([Index]), type number}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"pkBookingItemID", "StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "Index"}, {"pkBookingItemID.1", "StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "Index"}),
#"Added Custom" = Table.AddColumn(#"Expanded AllRows", "New_BusinessStartTime", each if [Index] = [MinIndex] then [StartTime] else [BusinessStartTime]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "New_BusinessEndTime", each if [Index] = [MaxIndex] then [EndTime] else [BusinessEndTime])
in
#"Added Custom1"
将上面的代码粘贴到高级查询编辑器中并查看。您将看到分组行和“AddIndexColumn”函数如何协同工作帮助我们选择第一行和最后一行。
然后,我们使用您的条件逻辑来构建 'new' 业务开始或结束时间。
希望对您有所帮助。
您是否在电力查询中使用导入模式? 单击您的 table 并复制您的 table(第一行和最后一行),然后右键单击您的特定列,然后 'remove other column' 复制此 table(第一次和最后一次) . 然后右键单击列旁边的 table 选项,然后选择 'Keep Top rows' 和“保留底行”。 然后使用追加查询将它们合并为一个 table 并将它们用作参数。
您可以在 Table.Group 函数中执行此操作,方法是创建一个新的 table 来获取第一个和最后一个条目的适当值。
例如:
原始数据
M码
...
//Group rows by ID and generate the new start/end columns
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"pkBookingItemID"}, {
{"NewTimes", each Table.FromColumns(
Table.ToColumns(_) &
{List.ReplaceRange([BusinessStartTime],0,1,{[StartTime]{0}}),
List.ReplaceRange([BusinessEndTime],Table.RowCount(_)-1,1,{[EndTime]{Table.RowCount(_)-1}})},
Table.ColumnNames(_) & {"NewBusinessStartTime","NewBusinessEndTime"})
}
}),
#"Expanded NewTimes" = Table.ExpandTableColumn(#"Grouped Rows", "NewTimes", {"StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "NewBusinessStartTime", "NewBusinessEndTime"}, {"StartTime", "EndTime", "BusinessStartTime", "BusinessEndTime", "NewBusinessStartTime", "NewBusinessEndTime"})
in
#"Expanded NewTimes"
结果