Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
InsightSeeker
Helper III
Helper III

Need help in creating new table and merging data based of Values

Hello everyone,

 

I require your assistance in consolidating data from multiple columns into a single table based on specific conditions. Instead of generating a calculated column, I intend to create a new table.

 

I have provided a PBIX sample data file for reference. You can access it here.

 

**Condition 1 (Column1):**

 

For each unique_id:

- Find the first Leg_Num and merge the corresponding From and To values into one column.

- Find the second Leg_Num and merge the corresponding From and To values into one column.

- Find the third Leg_Num and merge the corresponding From and To values into one column.

- Find the fourth Leg_Num and merge the corresponding From and To values into one column.

Continue this process...

 

Use "-" between From and To in the final result.

 

**Condition 2 (Column2):**

 

For each unique_id:

- Find the first Leg_Num and merge the corresponding From_Date and To_Date into one column.

- Find the second Leg_Num and merge the corresponding From_Date and To_Date into one column.

- Find the third Leg_Num and merge the corresponding From_Date and To_Date into one column.

- Find the fourth Leg_Num and merge the corresponding From_Date and To_Date into one column.

Continue this process...

 

Use "|" between From and To in the final result.

 

**Condition 3 (Column3):**

 

For each unique_id:

- Find the first Leg_Num and merge the Category with the second Leg_Num.

Continue this process...

 

Use "-" between From and To in the final result.

 

**Condition 4 (Column4):**

 

For each unique_id:

- Sum all Co2 values.

 

**Condition 5 (Column5):**

 

For each unique_id:

- Sum all KM values.

 

The resulting table should be displayed as follows:

 

unique_idNumber**Condition 1 (Column1):****Condition 2 (Column2):****Condition 3 (Column3):****Condition 4 (Column4):****Condition 5 (Column5):**
10958156073252842KGL-NBO05-Apr-24H31312471
10958286073252847DXB-KWI-DXB04-Apr-24|05-Apr-24K-K690111062
10958796073199119CAI-FCO-CAI12-Apr-24|17-Apr-24Z-Z775272672
10958806073199120CAI-FCO-CAI12-Apr-24|17-Apr-24Z-Z775272672
10958846073256049NBO-KGL03-Apr-24Q46215471
10959326073252871DXB-LHR-DXB29-Apr-24|04-May-24O-O988176842
10959366073252873CAI-DXB-CAI25-Apr-24|06-May-24O-O1073333006

 

InsightSeeker_0-1712399563405.png

 

 

@lbendlin 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @InsightSeeker 

Based on your description, you can create a new calculated table and input the following code.

Table =
SUMMARIZE (
    ADDCOLUMNS (
        Seg,
        "Column1",
            VAR __max_leg_num =
                CALCULATE ( MAX ( Seg[Leg_Num] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
            VAR __last_to =
                CALCULATE (
                    MAX ( Seg[To] ),
                    ALLEXCEPT ( Seg, Seg[unique_id] ),
                    Seg[Leg_Num] = __max_leg_num
                )
            RETURN
                CONCATENATEX (
                    TOPN (
                        __max_leg_num,
                        FILTER ( Seg, [unique_id] = EARLIER ( Seg[unique_id] ) ),
                        [Leg_Num]
                    ),
                    [From],
                    "-"
                ) & "-" & __last_to,
        "Column2",
            VAR __max_leg_num =
                CALCULATE ( MAX ( Seg[Leg_Num] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
            VAR __last_todate =
                CALCULATE (
                    MAX ( Seg[To_Date] ),
                    ALLEXCEPT ( Seg, Seg[unique_id] ),
                    Seg[Leg_Num] = __max_leg_num
                )
            VAR __last_fromdate =
                CALCULATE (
                    MAX ( Seg[From_Date] ),
                    ALLEXCEPT ( Seg, Seg[unique_id] ),
                    Seg[Leg_Num] = __max_leg_num
                )
            VAR _retultdate =
                IF ( __last_fromdate <> __last_todate, "|" & __last_todate )
            RETURN
                CONCATENATEX (
                    SUMMARIZE (
                        TOPN (
                            __max_leg_num,
                            FILTER ( Seg, [unique_id] = EARLIER ( Seg[unique_id] ) ),
                            [Leg_Num]
                        ),
                        [From_Date]
                    ),
                    [From_Date],
                    "|"
                ) & _retultdate,
        "Column3",
            VAR __max_leg_num =
                CALCULATE ( MAX ( Seg[Leg_Num] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
            RETURN
                CONCATENATEX (
                    TOPN (
                        __max_leg_num,
                        FILTER ( Seg, [unique_id] = EARLIER ( [unique_id] ) ),
                        [Leg_Num]
                    ),
                    [Category],
                    "-"
                ),
        "Column4", CALCULATE ( SUM ( Seg[Co2] ), ALLEXCEPT ( Seg, Seg[unique_id] ) ),
        "Column5", CALCULATE ( SUM ( Seg[KM] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
    ),
    Seg[unique_id],
    [Number],
    [Column1],
    [Column2],
    [Column3],
    [Column4],
    [Column5]
)

Output

vxinruzhumsft_0-1712718532632.png

Best Regards!

Yolo Zhu

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

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @InsightSeeker 

Based on your description, you can create a new calculated table and input the following code.

Table =
SUMMARIZE (
    ADDCOLUMNS (
        Seg,
        "Column1",
            VAR __max_leg_num =
                CALCULATE ( MAX ( Seg[Leg_Num] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
            VAR __last_to =
                CALCULATE (
                    MAX ( Seg[To] ),
                    ALLEXCEPT ( Seg, Seg[unique_id] ),
                    Seg[Leg_Num] = __max_leg_num
                )
            RETURN
                CONCATENATEX (
                    TOPN (
                        __max_leg_num,
                        FILTER ( Seg, [unique_id] = EARLIER ( Seg[unique_id] ) ),
                        [Leg_Num]
                    ),
                    [From],
                    "-"
                ) & "-" & __last_to,
        "Column2",
            VAR __max_leg_num =
                CALCULATE ( MAX ( Seg[Leg_Num] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
            VAR __last_todate =
                CALCULATE (
                    MAX ( Seg[To_Date] ),
                    ALLEXCEPT ( Seg, Seg[unique_id] ),
                    Seg[Leg_Num] = __max_leg_num
                )
            VAR __last_fromdate =
                CALCULATE (
                    MAX ( Seg[From_Date] ),
                    ALLEXCEPT ( Seg, Seg[unique_id] ),
                    Seg[Leg_Num] = __max_leg_num
                )
            VAR _retultdate =
                IF ( __last_fromdate <> __last_todate, "|" & __last_todate )
            RETURN
                CONCATENATEX (
                    SUMMARIZE (
                        TOPN (
                            __max_leg_num,
                            FILTER ( Seg, [unique_id] = EARLIER ( Seg[unique_id] ) ),
                            [Leg_Num]
                        ),
                        [From_Date]
                    ),
                    [From_Date],
                    "|"
                ) & _retultdate,
        "Column3",
            VAR __max_leg_num =
                CALCULATE ( MAX ( Seg[Leg_Num] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
            RETURN
                CONCATENATEX (
                    TOPN (
                        __max_leg_num,
                        FILTER ( Seg, [unique_id] = EARLIER ( [unique_id] ) ),
                        [Leg_Num]
                    ),
                    [Category],
                    "-"
                ),
        "Column4", CALCULATE ( SUM ( Seg[Co2] ), ALLEXCEPT ( Seg, Seg[unique_id] ) ),
        "Column5", CALCULATE ( SUM ( Seg[KM] ), ALLEXCEPT ( Seg, Seg[unique_id] ) )
    ),
    Seg[unique_id],
    [Number],
    [Column1],
    [Column2],
    [Column3],
    [Column4],
    [Column5]
)

Output

vxinruzhumsft_0-1712718532632.png

Best Regards!

Yolo Zhu

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

 

Hi @Anonymous  - The suggestion you provided is working as required, but I am encountering issues in a few cases when using this DAX in our database.

 

When the same logic is applied with the BI database connected to Excel, it works perfectly. However, when it comes to the SQL database, the same transactions do not work.

 

Is there anything that might be causing this issue?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.