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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Using Group By and Max + Sum function

Please help, I've table below.

 

PJ_CODEQuestionnaireSortQAnswerSortAQuestionnaire_TypeCustomer_IDTotalAnswerActivity_Date
J005Purpose05C0503Visit381630111/12/2023
J005Purpose05A0504Visit4273550.511/12/2023
J005Purpose05B0501Visit4273550.511/12/2023
J005Purpose05B0501Visit188237110/12/2023
J005Purpose05A0504Visit4267660.509/12/2023
J005Purpose05C0503Visit4267660.509/12/2023
J005Purpose05B0501Visit402564108/12/2023
J005Purpose05B0501Visit427473107/12/2023
J005Purpose05B0501Visit426955105/12/2023
J005Purpose05B0501Visit427441105/12/2023
J005Purpose05A0504Visit4267660.504/12/2023
J005Purpose05C0503Visit4267660.504/12/2023

and I want to create dax measure about visualize matrix to group by Answer and sum of TotalAnswer and order by the MAX of Activity_Date.

 

My Expected of Matrix below.

Group by Answer and order by the MAX of Activity_Date.

 

AnswerSum of TotalAnswer
A1
B5.5
C1.5
Total8

 

4.png

 

As picture below, I've already create relationship of TEMP_WQ. 

 

2.png

 

 

I use such a

Dax:
Previous1 =
VAR StartDate = MIN(DimDate[Date]) /* User can select Date as (Number 2) */
VAR EndDate = MAX(DimDate[Date]) /* User can select Date as (Number 2) */
VAR NumDays = MAX(0, EndDate - StartDate + 1) /* Count of date that selected */
VAR PreviousStartDate = StartDate - NumDays /* Need to focus of PreviosDate */
VAR PreviousEndDate = EndDate - NumDays /* Need to focus of PreviosDate */
Var PJ = VALUES(DimProject[PJ_CODE]) /* User can select Project as (Number 1) */
Return CALCULATE(SUM(TEMP_WQ[TotalAnswer])

,GROUPBY(TEMP_WQ,TEMP_WQ[Questionnaire_Type],TEMP_WQ[Questionnaire],TEMP_WQ[Answer]),
FILTER(
ALL(TEMP_WQ),
TEMP_WQ[Activity_Date] >= PreviousStartDate &&
TEMP_WQ[Activity_Date] <= PreviousEndDate &&
TEMP_WQ[PJ_CODE] IN PJ &&
TEMP_WQ[Questionnaire_Type] = "Visit"
)
)

 

But Result was wrong.

AnswerSum of TotalAnswer
B5.5
Total5.5
2 REPLIES 2
Anonymous
Not applicable

Hi @Anonymous ,

 

Please try:

SumTotalAnswer = SUM('TEMP_WQ'[TotalAnswer])

MaxActivityDate = MAX('TEMP_WQ'[Activity_Date])

 

I do not quite understand "order by the MAX of Activity_Date.".

 

I realized your expected result.

vhuijieymsft_0-1709688620271.png

 

The pbix file is attached.

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

Anonymous
Not applicable

Hi @Anonymous ,

 

Thank you for your help. tried the following DAX expression for the Previous1.

 

Previous1 = 
Var PJ = VALUES(DimProject[PJ_CODE]) 
Return CALCULATE(SUM(TEMP_WQ[TotalAnswer])
,GROUPBY(TEMP_WQ,TEMP_WQ[Questionnaire_Type],TEMP_WQ[Questionnaire],TEMP_WQ[Answer]),
FILTER(
ALL(TEMP_WQ),
TEMP_WQ[Activity_Date] >= PreviousStartDate &&
TEMP_WQ[Activity_Date] <= PreviousEndDate &&
TEMP_WQ[PJ_CODE] IN PJ &&
TEMP_WQ[Questionnaire_Type] = "Visit")
)

 

 

But this didnt work.

 

AnswerPrevios1
B5.5
Total5.5

 

My Expected Result.

AnswerPrevios1
A1
B5.5
C1.5
Total8

 

So, I try to create SQL script to expain my expected.

 

Sub TableSub TableMy Expected ResultMy Expected Result

 

 

If you have any question please feel free to contact me.

Niramol L.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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