Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Pivotting a dax created custom table

Hi ALl,

 

I've created a custom table in dax using the following code:

WeeklyStages = 
ADDCOLUMNS(
CROSSJOIN(
VALUES('2Weeks_Oppo_WeeklyChanges'[Salesforce_Id__c]),
{"Stage Last Week", "Stage This Week"}
),
"Stage",
VAR _rank = [Value]
VAR _name = [Salesforce_Id__c]
VAR _rank2 = MAXX(
FILTER(
'2Weeks_Oppo_WeeklyChanges',
'2Weeks_Oppo_WeeklyChanges'[Salesforce_Id__c] = _name && '2Weeks_Oppo_WeeklyChanges'[Rank] = 2
),
[Stage]
)
VAR _rank1 = MAXX(
FILTER(
'2Weeks_Oppo_WeeklyChanges',
'2Weeks_Oppo_WeeklyChanges'[Salesforce_Id__c] = _name && '2Weeks_Oppo_WeeklyChanges'[Rank] = 1
),
[Stage]
)
RETURN IF(_rank = "Stage Last Week", _rank2, _rank1)
)

 

This creates the following table:

 

 

 

I want to pivot this data so that it shows as the following:

Salesforce_id_c, Stage Last Week, Stage This Week

 

The values in the stage column would be used to populate the "Stage Last Week" and "Stage This Week" columns.

 

A fairly routine pivot, however the fact that its a custom table is confusing me. Any tips?

 

  • Hi, Anonymous 

    According to your description, you want to pivot a calculated column .Here are the steps you can refer to 

    (1)This is my test data:

    (2)We can click "New Table" and enter this dax code:

    Table = ADDCOLUMNS( VALUES('WeeklyStages'[Salesforce_id_c]),"Stage Last Week",
    CALCULATE(MAX('WeeklyStages'[Stage]) ,'WeeklyStages'[Salesforce_id_c]=EARLIER([Salesforce_id_c]) && 'WeeklyStages'[Value]="Stage Last Week"
    ),
    "Stage This Week",
    CALCULATE(MAX('WeeklyStages'[Stage]) ,'WeeklyStages'[Salesforce_id_c]=EARLIER([Salesforce_id_c]) && 'WeeklyStages'[Value]="Stage This Week"
    
    ))

     

    Then we can get this table :

     

     

    If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem. (You can also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )

    Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

     

    Best Regards,

    Aniya Zhang

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

     

     

     

2 Replies

  • Hi, Anonymous 

    According to your description, you want to pivot a calculated column .Here are the steps you can refer to 

    (1)This is my test data:

    (2)We can click "New Table" and enter this dax code:

    Table = ADDCOLUMNS( VALUES('WeeklyStages'[Salesforce_id_c]),"Stage Last Week",
    CALCULATE(MAX('WeeklyStages'[Stage]) ,'WeeklyStages'[Salesforce_id_c]=EARLIER([Salesforce_id_c]) && 'WeeklyStages'[Value]="Stage Last Week"
    ),
    "Stage This Week",
    CALCULATE(MAX('WeeklyStages'[Stage]) ,'WeeklyStages'[Salesforce_id_c]=EARLIER([Salesforce_id_c]) && 'WeeklyStages'[Value]="Stage This Week"
    
    ))

     

    Then we can get this table :

     

     

    If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem. (You can also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )

    Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

     

    Best Regards,

    Aniya Zhang

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

     

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      thank you, this done the trick 😄