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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Pivot the column in Power Query based on condition

Hi,

I have the following data in power Query

deedeedudu_0-1662716071717.png

I want to transform it to the following:

deedeedudu_1-1662716116759.png

 

Following logic to be used:

1. First message for the contact_id of the day with interaction as incoming should be moved to Keyword field and the date should be entered in the datetime column

2. Subsequent messages of the day for that contact should be recorded in either Qn or Rn basis if the interaction is outgoing or incoming respectively.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

I have created a simple sample, please refer to it to see if it helps you.

First, add an index column.

Then create a column.

result =
VAR _1 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "incoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index]
    )
VAR _Q1 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outgoing"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index]
    )
VAR _R1 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "incoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 2
    )
VAR _Q2 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outgoing"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 2
    )
VAR _R2 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "incoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 4
    )
VAR _Q3 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outgoing"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 4
    )
VAR _R3 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outcoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 6
    )
RETURN
    IF (
        _1 = 'Table'[Index],
        "keyword",
        IF (
            _Q1 = 'Table'[Index],
            "Q1",
            IF (
                _R1 = 'Table'[Index],
                "R1",
                IF (
                    _Q2 = 'Table'[Index],
                    "Q2",
                    IF (
                        _R2 = 'Table'[Index],
                        "R2",
                        IF ( _Q3 = 'Table'[Index], "Q3", IF ( _R3 = 'Table'[Index], "R3", BLANK () ) )
                    )
                )
            )
        )
    )

vpollymsft_0-1662965428464.png

Best Regards

Community Support Team _ Polly

 

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 @Anonymous ,

I have created a simple sample, please refer to it to see if it helps you.

First, add an index column.

Then create a column.

result =
VAR _1 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "incoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index]
    )
VAR _Q1 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outgoing"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index]
    )
VAR _R1 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "incoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 2
    )
VAR _Q2 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outgoing"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 2
    )
VAR _R2 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "incoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 4
    )
VAR _Q3 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outgoing"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 4
    )
VAR _R3 =
    MINX (
        FILTER (
            'Table',
            'Table'[interaction] = "outcoming"
                && 'Table'[contact_id] = EARLIER ( 'Table'[contact_id] )
                && 'Table'[datetime] = EARLIER ( 'Table'[datetime] )
        ),
        'Table'[Index] + 6
    )
RETURN
    IF (
        _1 = 'Table'[Index],
        "keyword",
        IF (
            _Q1 = 'Table'[Index],
            "Q1",
            IF (
                _R1 = 'Table'[Index],
                "R1",
                IF (
                    _Q2 = 'Table'[Index],
                    "Q2",
                    IF (
                        _R2 = 'Table'[Index],
                        "R2",
                        IF ( _Q3 = 'Table'[Index], "Q3", IF ( _R3 = 'Table'[Index], "R3", BLANK () ) )
                    )
                )
            )
        )
    )

vpollymsft_0-1662965428464.png

Best Regards

Community Support Team _ Polly

 

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

lbendlin
Super User
Super User

Please provide sanitized sample data that fully covers your issue.
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Solution Authors