Forum Discussion

EaglesTony's avatar
EaglesTony
Post Prodigy
2 years ago
Solved

How do I get values from a 2nd table to another table?

hi,

  I have this first table called Features and has :

 

ID                              StartDate     DueDate

ABC-123                   4/1/2024      7/31/2024

ABC-456                   1/24/2024    4/23/2024

ABC-982                   3/1/2024      <blank>

ABC-012                   <blank>       3/1/2024

 

The 2nd table called Quarters has:

 

 Quarter     StartDate       EndDate

2024Q1      1/8/2024        3/26/2024

2024Q2      3/27/2024      7/2/2024

2024Q3      7/3/2024        9/24/2024

2024Q4      9/25/2024      12/31/2024

 

The result I want is the maybe another table to be

 

ID                                      

ABC-123                   2024Q2

ABC-456                   2024Q1   2024Q2

ABC-982                   2024Q1

ABC-012                   2024Q1

 

I need each item in the Feature table to have all the quarters it falls in.

  • Hi EaglesTony,

    You can try the following approach in power query:

    starting tables

    In Features table we run these transformations (paste in advanced editor):

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRy1jU0MlbSUTIw1Dcw0TcyMDIBcoyBHHMIJ1YHosrE1AwoYWSiD1QIVWVkDNcCU2VpYQQ1yximKq80Jwcub2BoBBNCURYbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, StartDate = _t, DueDate = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type date}, {"DueDate", type date}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Quarters",
    (f) => Table.SelectRows(
        Quarters,
        (q) => f[StartDate] >= q[StartDate] and f[StartDate] <= q[EndDate]
        or f[DueDate] >= q[StartDate] and f[DueDate] <= q[EndDate]
        )[Quarter]
    ),
        #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Quarters", each Text.Combine(List.Transform(_, Text.From), "; "), type text}),
        #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"StartDate", "DueDate"})
    in
        #"Removed Columns"

     

     

    And that's our output:

     

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi EaglesTony 

     

    Just as wini_R posted that you can get the outcome in power query.

    And here is another method with dax:

    First of all, I create a new table:

    Table 2 =
    FILTER (
        CROSSJOIN ( 'Table', 'Table (2)' ),
        OR (
            'Table'[StartDate] >= 'Table (2)'[Start]
                && 'Table'[StartDate] <= 'Table (2)'[End],
            'Table'[DueDate] >= 'Table (2)'[Start]
                && 'Table'[DueDate] <= 'Table (2)'[End]
        )
    )

    Then add a measure:

    Measure = CONCATENATEX('Table 2','Table 2'[Quarter],";")

    The result is as follow:

    Best Regards

    Zhengdong Xu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • wini_R's avatar
    wini_R
    Solution Supplier

    Hi EaglesTony,

    You can try the following approach in power query:

    starting tables

    In Features table we run these transformations (paste in advanced editor):

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRy1jU0MlbSUTIw1Dcw0TcyMDIBcoyBHHMIJ1YHosrE1AwoYWSiD1QIVWVkDNcCU2VpYQQ1yximKq80Jwcub2BoBBNCURYbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, StartDate = _t, DueDate = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type date}, {"DueDate", type date}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Quarters",
    (f) => Table.SelectRows(
        Quarters,
        (q) => f[StartDate] >= q[StartDate] and f[StartDate] <= q[EndDate]
        or f[DueDate] >= q[StartDate] and f[DueDate] <= q[EndDate]
        )[Quarter]
    ),
        #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Quarters", each Text.Combine(List.Transform(_, Text.From), "; "), type text}),
        #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"StartDate", "DueDate"})
    in
        #"Removed Columns"

     

     

    And that's our output:

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi EaglesTony 

     

    Just as wini_R posted that you can get the outcome in power query.

    And here is another method with dax:

    First of all, I create a new table:

    Table 2 =
    FILTER (
        CROSSJOIN ( 'Table', 'Table (2)' ),
        OR (
            'Table'[StartDate] >= 'Table (2)'[Start]
                && 'Table'[StartDate] <= 'Table (2)'[End],
            'Table'[DueDate] >= 'Table (2)'[Start]
                && 'Table'[DueDate] <= 'Table (2)'[End]
        )
    )

    Then add a measure:

    Measure = CONCATENATEX('Table 2','Table 2'[Quarter],";")

    The result is as follow:

    Best Regards

    Zhengdong Xu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.