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 )
Related
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:
I have the dataset:
I've checked the occurrence of each id and if it was the first occurrence per the id, I assigned the value 0, otherwise 1.
If I create a pivot table and sum of occurrence, I will get :
So, my final desired outcome is:
I can achieve it with the countrows and sum of countrows as a calculated column but it is static and as soon as I start using the date filter, the formula doesn't work. Is there a way to achieve it with a measure?
Create these below 2 Measures-
Note: Considered ordering using column ticket_id
occ =
VAR current_ticket_id = MIN(your_table_name[ticket_id])
VAR current_id = MIN(your_table_name[id])
VAR count_id =
CALCULATE(
COUNTROWS(your_table_name),
FILTER(
ALL(your_table_name),
your_table_name[id] = current_id
&& your_table_name[ticket_id] <= current_ticket_id
)
)
RETURN
IF(
count_id = 1,
0,
1
)
sum of occ =
CALCULATE(
COUNTROWS(your_table_name),
FILTER(
ALLEXCEPT(your_table_name,your_table_name[id]),
[occ] = 1
)
) + 0
Here is the output-
Situation:
I have a column (refund) that takes the MAX value for each day so that I don't duplicate the refunds.
refund =
var calcul =
CALCULATE(
SUM(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
This works for each day but the total is the max of that column
Objective:
I need this measure to calculate the net sales. I have the gross sales measure already but my refund formula doesn't work when aggregated.
What i tried (thanks to Alexis Olson):
refund =
var calcul =
CALCULATE(
SUM(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
return
SUMX(DISTINCT(Query1[orderId]), calcul)
However the output is unexpected. It basically takes each value and multiplies it by the distinct count of order ids on that day (refund or not).
So i tried dividing it by the distinct count of order id but the same problem with the total row taking the max value occurs.
Here's the output i get using the provided solution below:
Relationships:
Query1[created_at] DateTable[Date] (inactive)
Query1[orderDate] DateTable[Date] (active)
My returns measure:
Returns =
CALCULATE(
MAX(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
You're pretty close to something that works. Try changing calcul to a summary table instead of a single value scalar like this:
refund =
VAR Summary =
SUMMARIZE (
Query1,
Query1[orderId],
"MaxValue", CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[created_at], DateTable[Date] ),
Query1[kind] = "refund",
Query1[status] = "success"
)
)
RETURN
SUMX ( Summary, [MaxValue] )
One way of fixing my issue was to use CALCULATETABLE before the summarize and then
use the SUMX as suggested above by Alexis.
Returns =
VAR sumary =
CALCULATETABLE(
SUMMARIZE(
Query1,
Query1[orderId],
"maxValue",CALCULATE(
MAX(Query1[amount]),
Query1[kind]= "refund",
Query1[status] = "success"
)
),USERELATIONSHIP(Query1[trx_date],DateTable[Date]))
RETURN
SUMX(sumary,[maxValue])
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.