kenjang cool
kenjang cool

Reputation: 11

Using Group By and Max + Sum function

I have a table like below:

PJ_CODE Questionnaire SortQ Answer SortA Questionnaire_Type Customer_ID TotalAnswer Activity_Date
J005 Purpose 05 A 0504 Visit 426766 0.5 04/12/2023
J005 Purpose 05 C 0503 Visit 426766 0.5 04/12/2023
J005 Purpose 05 B 0501 Visit 426955 1 05/12/2023
J005 Purpose 05 B 0501 Visit 427441 1 05/12/2023
J005 Purpose 05 B 0501 Visit 427473 1 07/12/2023
J005 Purpose 05 B 0501 Visit 402564 1 08/12/2023
J005 Purpose 05 A 0504 Visit 426766 0.5 09/12/2023
J005 Purpose 05 C 0503 Visit 426766 0.5 09/12/2023
J005 Purpose 05 B 0501 Visit 188237 1 10/12/2023
J005 Purpose 05 C 0503 Visit 381630 1 11/12/2023
J005 Purpose 05 A 0504 Visit 427355 0.5 11/12/2023
J005 Purpose 05 B 0501 Visit 427355 0.5 11/12/2023

I have duplicate values of the Answer and Customer_ID, so I want to group by Answer , Customer_ID and Max of Last (Activity_Date) and then calculate the total of TotalAnswer.

After Grouping:

Answer Customer_ID TotalAnswer Activity_Date
A 426766 0.5 09/12/2023
A 427355 0.5 11/12/2023
B 188237 1 10/12/2023
B 402564 1 08/12/2023
B 426955 1 05/12/2023
B 427355 0.5 11/12/2023
B 427441 1 05/12/2023
B 427473 1 07/12/2023
C 381630 1 11/12/2023
C 426766 0.5 09/12/2023

The final Value. Total Measure =

Answer Total Measure
A 1
B 5.5
C 1.5
Total 8

I want to create dax measure about visualize matrix : row is Answer and Value is Total Measure.

I tried the following DAX expression for the Total Measure:

Total Measure = 
VAR StartDate = MIN(DimDate[Date]) /* Mark Relationship of DimDate */
VAR EndDate = MAX(DimDate[Date]) /* Mark Relationship of DimDate */
VAR NumDays = MAX(0, EndDate - StartDate + 1)
VAR PreviousStartDate = StartDate - NumDays
VAR PreviousEndDate = EndDate - NumDays
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 either. For every help thanks in advance.

Answer Previous1
B 5.5
Total 5.5

I've attach sample SQL query to display my Expected Result. I try to create dax like a SQL query but didn't work.

enter image description here

enter image description here

Upvotes: 1

Views: 53

Answers (0)

Related Questions