【问题标题】:Power BI - DateTime list - many to many query mergePower BI - 日期时间列表 - 多对多查询合并
【发布时间】:2020-11-10 20:59:59
【问题描述】:

假设您有 2 个表:

  1. 包含 Start、End 和 ID 列的数据表(ID 不同)。

开始、结束、ID
2020-01-01 00:01:00,2020-01-01 00:01:05,1111-1111111-1111-11-11
2020-01-01 00:01:00,2020-01-01 00:05:00,1111-1111111-1111-11-12
2020-01-01 00:02:00,2020-01-01 00:03:05,1111-1111111-1111-11-13
2020-01-01 00:10:00,2020-01-01 00:11:00,1111-1111111-1111-11-14

  1. 带有使用 List.DateTimes(#datetime(2020, 01, 01, 00, 01, 0), 10, #duration(0, 0, 1, 0)) 创建的列表的 DataTime 表

2020-01-01 00:01:00
2020-01-01 00:02:00
2020-01-01 00:03:00
2020-01-01 00:04:00
2020-01-01 00:05:00
2020-01-01 00:06:00
2020-01-01 00:07:00
2020-01-01 00:08:00
2020-01-01 00:09:00
2020-01-01 00:10:00

我想在 M 查询级别合并这些表,因此如果 Start 大于或等于第二个表中的 DateTime 并且 End 小于或等于第二个表中的 DateTime,则每个 ID 有 1 行.预期结果:

DateItme,ID
2020-01-01 00:01:00,1111-1111111-1111-11-11
2020-01-01 00:01:00,1111-1111111-1111-11-12
2020-01-01 00:02:00,1111-1111111-1111-11-12
2020-01-01 00:03:00,1111-1111111-1111-11-12
2020-01-01 00:04:00,1111-1111111-1111-11-12
2020-01-01 00:05:00,1111-1111111-1111-11-12
2020-01-01 00:06:00,1111-1111111-1111-11-12
2020-01-01 00:02:00,1111-1111111-1111-11-13
2020-01-01 00:03:00,1111-1111111-1111-11-13
2020-01-01 00:06:00
2020-01-01 00:07:00
2020-01-01 00:08:00
2020-01-01 00:09:00
2020-01-01 00:10:00,1111-1111111-1111-11-14

请帮忙:)!

【问题讨论】:

    标签: datetime merge powerbi m


    【解决方案1】:

    这可以用M语言制作;我们需要添加 1 作为“虚拟”列

    我的查询1(日期表)

        let
            Source = List.DateTimes(#datetime(2020, 01, 01, 00, 01, 0), 10, #duration(0, 0, 1, 0)),
            #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
            #"Added Custom" = Table.AddColumn(#"Converted to Table", "dummy", each 1),
            #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Column1", "Date"}}),
            #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"dummy", type text}})
        in
            #"Changed Type"
    

    还有我的表定义:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY6xCQAxDANXeVzHIDmfJqsE77/GG5JShBdCxRWH1rJAwMHqA0xwAtYEHUVZce74mapl++kZm2pPKE9IT7/96cJDKA95+/Na5gc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Start = _t, End = _t, ID = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start", type datetime}, {"End", type datetime}, {"ID", type text}}),
        #"Inserted Text Before Delimiter" = Table.AddColumn(#"Changed Type", "dummy", each Text.BeforeDelimiter([ID], "1", 1), type text),
        #"Merged Queries" = Table.NestedJoin(#"Inserted Text Before Delimiter", {"dummy"}, Query1, {"dummy"}, "Query1", JoinKind.LeftOuter),
        #"Added Custom" = Table.AddColumn(#"Merged Queries", "Custom", each let mydate= [Start] , mydate2 = [End] in 
    Table.SelectRows([Query1],each [Date] <= mydate2 and [Date] >= mydate)),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Date", "dummy"}, {"Custom.Date", "Custom.dummy"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Query1"})
    in
        #"Removed Columns"
    

    最重要的一行是这一行:

    #"Added Custom" = Table.AddColumn(#"Merged Queries", "Custom", each let mydate= [Start] , mydate2 = [End] in 
    Table.SelectRows([Query1],each [Date] <= mydate2 and [Date] >= mydate))
    

    【讨论】:

    • 天才!我会在检查后告诉你:)
    猜你喜欢
    • 2023-02-08
    • 2020-04-22
    • 2019-04-11
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2021-11-17
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多