We use this SQL statement for getting top data value in a column
SELECT TOP 1 NOTE_DETAIL
FROM CLAIM_NOTES
WHERE CLAIM_NO = C.CLAIM_NO AND ISNULL(DELETED, 0) = 0
ORDER BY CREATED_DATE DESC
How we can we use this approach in a DAX query if we want last_note from note detail?
For your data:
The DAX measure:
Last Note Detail =
VAR MaxCreatedDate =
CALCULATE (
MAX ( CLAIM_NOTES[CREATED_DATE] ),
ALLEXCEPT ( CLAIM_NOTES, CLAIM_NOTES[CLAIM_NO] )
)
VAR Result =
CALCULATE (
MAX ( CLAIM_NOTES[NOTE_DETAIL] ),
CLAIM_NOTES[CREATED_DATE] = MaxCreatedDate
)
RETURN
Result
Gives a visual of:
Related
While executing the below DAX expression, I am getting an error "USERELATIONSHIP function can only use the two columns reference participation in relationship".
So could you please help me with that what's wrong with the expression?
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX(Calender[Date])
VAR Last_6Month =
DATESINPERIOD(
Calendar_Last6Month[Date].[Date],
ReferenceDate,
-6,
MONTH
)
VAR Result =
CALCULATE(
[Accuracy],
REMOVEFILTERS(Calender[Date]),
KEEPFILTERS(Last_6Month),
USERELATIONSHIP(Calender[Date],Calendar_Last6Month[Date].[Date])
)
RETURN
Result
Relationship created between tables as inactivated form:
Columns used in both the table:
You should be able to use a single Calendar. Your second calendar is redundant.
I would write something like this:
Accuracy_Last_6_Month =
CALCULATE([Accuracy],
FILTER(ALL(Calender),
Calender[Date] > MAX(Calender[Date])-180 &&
Calender[Date] <= MAX(Calender[Date])))
I'm guessing the error is because you are using Calendar_Last6Month[Date].[Date] inside USERELATIONSHIP, which isn't actually a table column. Try deleting that .[Date] suffix everywhere in your measure:
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX ( Calender[Date] )
VAR Last_6Month =
DATESINPERIOD ( Calendar_Last6Month[Date], ReferenceDate, -6, MONTH )
VAR Result =
CALCULATE (
[Accuracy],
REMOVEFILTERS ( Calender[Date] ),
KEEPFILTERS ( Last_6Month ),
USERELATIONSHIP ( Calender[Date], Calendar_Last6Month[Date] )
)
RETURN
Result
I generally avoid using these Time Intelligence suffixes entirely.
I have WTDdata table that contains ThisYearRevenue and LastYearRevenue summarized by week:
I need to create 4 more columns LastYearOnlineRevenue, LastYearStoreRevenue, ThisYearOnlineRevenue, and ThisYearStoreRevenue from another table (RevenueByDate) that looks like this:
W column in this table means Fiscal Week.
I tried using this aproach:
LastYearOnlineRevenue =
SUMMARIZE(FILTER(ALL(FiscalCalendar),FiscalCalendar[FiscalWeek]),FiscalCalendar[FiscalWeek]),
"LastYearOnlineRevenue",CALCULATE(SUM(RevenueByDate[Revenue]),FiscalCalendar[FiscalYear] =
YEAR(TODAY())-1 && RevenueByDate[Channel] = "Online")
If you can help me with at least one column, I am assuming the logic will be the same for the other 3.
Thank you in advance.
Here is the location for sample data and .pbix file:
https://1drv.ms/u/s!AhhZq1add5YwjYIvuASi76lCL3R1eA?e=C7ObDZ
Try this:
Since there is no way of telling the current year from the WTDdata table, I have assumed that the current year is always 2021, you can also replace that with:
VALUE ( MAX ( RevenueByDate[FiscalYear] ) )
LastYearOnlineRevenue =
VAR CurrentFiscalWeek = WTDdata[FiscalWeek]
VAR CurrentYear = 2021
VAR ImmediatePreviousYear =
CALCULATE (
MAX ( FiscalCalendar[FiscalYear] ),
FiscalCalendar[FiscalYear] < CurrentYear,
REMOVEFILTERS ( WTDdata )
)
VAR Result =
CALCULATE (
SUM ( RevenueByDate[Revenue] ),
FiscalCalendar[FiscalWeek] = CurrentFiscalWeek,
FiscalCalendar[FiscalYear] = ImmediatePreviousYear,
RevenueByDate[Channel] = "Online",
REMOVEFILTERS ( WTDdata )
)
RETURN
Result
I have several dataset tables in PowerBI report. The column country comes from TABLE1 while the column name comes from TABLE2.
So firstly I want to calculate min_number based on country and name, and then if min_number = number, the min will be 1; otherwise, 0. So the result table looks like:
This is my code for min
min =
VAR min_number =
CALCULATE (
MIN ( [number] ),
ALLEXCEPT ( TABLE1, TABLE1[country] ), ALLEXCEPT (TABLE2, TABLE2[name])
)
RETURN
IF ( [number] = Min_number,1, 0 )
I got an error: the MIN function only accepts a column reference as the argument number 1. Does it mean if it has to be one condition? how to fix it? Thank you
I would solve it by just making two separate measures, since we want to see the both results in the final table anyway.
First the min_number calculation:
min_number = CALCULATE(MIN('Table'[number]);ALLEXCEPT('Table';'Table'[country];'Table'[name]))
And the min measure:
min = IF(MAX('Table'[number]) = [min_number];1;0)
As we are using a measure, we can use MAX, so it will know what number to reference in the IF. It will still use the MAX number per row, so results are correct.
You can try with this below measure-
min =
VAR current_row_country = MIN(table1[country])
VAR current_row_name = MIN(table1[name])
VAR current_row_number = MIN(table1[number])
VAR min_number =
CALCULATE (
MIN (table1[number]),
FILTER(
ALL(table1),
table1[country] = current_row_country
&& table1[name] = current_row_name
)
)
RETURN IF (min_number = current_row_number,1, 0 )
I have the following Table Visualization.
I'd like the table to look like the following. Column C should be averaging the range of Column B.
For example:
C2 = AVERAGE(B2:B2)
C3 = AVERAGE(B2:B3)
C4 = AVERAGE(B2:B4)
and so on.
The Year-Month column is from my MonthTable. The schema is as follows,
And the Sum measure DAX is as follows,
For the CumulativeSum measure, I have tried the following.
CumulativeSum =
CALCULATE(
[Sum],FILTER(AppendedTables,AppendedTables[Year-Month] <= MAX(AppendedTables[Year-Month]))
)
I'm guessing the issue is my CALCULATE([SUM]) area. I wanted to wrap [SUM] in a SUM() method, but that doesn't work. It gives the error "The SUM function only accepts a column reference as the argument number 1".
Please enlighten me.
I've been able to produce your desired results by creating a calculated column using the following code:
CumSum =
VAR CntRow =
COUNTROWS ( FILTER ( Sheet1, [Year-Month] >= EARLIER ( [Year-Month] ) ) )
VAR CumSum =
CALCULATE (
SUMX ( Sheet1, Sheet1[Sum] ),
FILTER ( Sheet1, Sheet1[Year-Month] >= EARLIER ( Sheet1[Year-Month] ) )
)
RETURN
DIVIDE ( CumSum, CntRow )
Hope this helps!!
How to use if else for DAX in the measure. If row value =1 then take the var a calculated value else take the var b calculated value
x:=var a=[DATA1]
var b=[DATA2]
return(if([HOUR]=1),a,b)
I get error using above formula
It seems your problem is that you are not aggregating the columns while creating the measure. Measures only works aggregating data in a given context, generally if you want to perform calculations per row you should use a calculated column instead of a measure.
And the DAX expression for a calculated column should be:
MyColumn = IF([HOUR] = 1, [DATA1], [DATA2])
Otherwise if you want to use a measure you have to explicitely aggregate the column values in the given context, i.e:
MyMeasure =
VAR a =
FIRSTNONBLANK ( ExampleTable[Data1], 0 )
VAR b =
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
RETURN
IF ( SUM ( ExampleTable[Hour] ) = 1, a, b )
Or simply:
MyMeasure =
IF (
SUM ( [Hour] ) = 1,
FIRSTNONBLANK ( ExampleTable[Data1], 0 ),
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
)
Let me know if this helps.