I'm trying to create a new mesure in order to create a gantt chart, but the dax field keeps spitting that there's an error. Where in fact i literally copied the same code in the tutorial i'm following.
And the new mesure is as follows:
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date ],
REMOVEFILTERS(TabelaCalculada)
), var EndDate =
CALCULATE(
min(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var ProjectPeriod =
min(TabelaCalculada[Date]) >= startdate
&& min(TabelaCalculada[Date]) <= startdate
var result =
if(
TabelaCalculada,
1
)
return
You see PBI is saying the following: "the syntax for '&&' is incorrect", i even searched the documentation for this particular AND method but i don't see how it's wrong.
This DAX is invalid in multiple ways which I point out on the assumption that you are trying to learn what valid DAX is.
Comma after the definition of var startdate.
Project Period is a bunch of filters with no actual function.
Project Period is trying to filter on an aggregate, which is not allowed.
return statement is missing the return argument
var result is incomprehensible.
This will return a 1 for each row where the date falls between minimum Start Date and maximum End Date, per your latest comment.
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date]),
REMOVEFILTERS(TabelaCalculada)
)
var EndDate =
CALCULATE(
max(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var result = if(
selectedvalue(TabelaCalculada[Date] >= startdate
&& TabelaCalculada[Date] <= EndDate,1)
)
return result
I've found a third syntax alternative that worked. So my goal as i explained was to create a Gantt Chart using the matrix innate visual and for that one way to do it is to create a measure that returns a 1 if i have a date that's either on or after the starting date and on or before the ending date.
BetweenDates =
VAR beginning = min ( Contracts_range[Start Date])
VAR finish = max ( Contracts_range[End Date] )
VAR duration = max ( Contracts_range[Duration] )
VAR colorVar = 0
RETURN
SWITCH (
TRUE(),
AND ( MAX ( 'Calendar'[Date] ) >= beginning , MIN ( 'Calendar'[Date] ) <= finish ) && duration >0,1
)
Related
I have a measure that totals the values for each date in the table. I want to filter this measure so that I can display only the last 28 days present in the table instead of displaying values for all the dates. Following is the code that works for getting totals for full table:
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) ) )
The 'relative date' filter in the Filters pane does not work because it only accepts the last 28 days based on today's date and not the dates in the table. Please suggest a DAX formula that can filter for the last 28 days present in the table.
Try this code
VAR endDay = LastDate(Daily_Reports[Case_Date])
VAR startDay= DATEADD(endDay,-28,DAY)
VAR setOfDates = DATESBETWEEN(Daily_Reports[Case_Date], StartDate, EndDate )
RETURN
CALCULATE(
SUM(Daily_Reports[Confirmed])
,setOfDates
)
You can try this one:
MS =
CALCULATE (
SUM ( Daily_Reports[Confirmed] ),
FILTER (
ALL ( Daily_Reports[Case_Date] ),
Daily_Reports[Case_Date]
>= SELECTEDVALUE ( Daily_Reports[Case_Date] ) - 28
)
)
This is what finally worked for me. I created a measure (not a column), that returns 1 for the last 28 days with an IF clause, leaving it blank if the date is not in the last 28 days as follows:
Last28 =
VAR MaxDate = LASTDATE( ALL(Daily_Reports[Case_Date]) )
VAR MinDate = DATEADD( MaxDate, -28, DAY )
RETURN
IF( SELECTEDVALUE(Daily_Reports[Case_Date]) >= MinDate && SELECTEDVALUE(Daily_Reports[Case_Date]) <= MaxDate, 1 )
Then I incorporated this measure into the Calculate function as follows:
Daily Cases =
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) && NOT(ISBLANK(Daily_Reports[Last28]))
)
)
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 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 )
Need some help to calculate the following in two separate columns in a DAX formula
Earliest Reading for each equip
Latest Reading for each equip
Screenshot of sheet
I've been able to get the first date of each equip with this.
CALCULATE(FIRSTDATE(Transactions[Date]),ALLEXCEPT(Transactions,Transactions[Equip No]))
But cannot work out how to get the 'Reading' value that is associated with the first date
I've managed to do this with a measure, but would also like to get this in a calc. column.
Latest Reading =
SUMX (
VALUES( Transactions[Equip No] ),
CALCULATE ( MIN ( Transactions[Reading] ), FIRSTDATE ( Transactions[Date] ) )
)
this should help you.
Earliest Column
Earliest =
VAR __equipNumber = 'Transaction'[Equip No] //Get the Equip No to filter the main table and create an auxiliar table for every different Equip No.
VAR __minDate = CALCULATE( MIN('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) ) //Get the lowest date asociated to every Equip No.
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __minDate ) //Create a table that contains 1 row asociate to the lowest date.
Return CALCULATE(SUM('Transaction'[Reading]), __subTable) //Operate over the auxiliar table to get the expected value.
Latest Column
Latest =
VAR __equipNumber = 'Transaction'[Equip No]
VAR __maxDate = CALCULATE( MAX('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) )
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __maxDate )
Return CALCULATE(SUM('Transaction'[Reading]), __subTable)
I obtained the expected result
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])