DAX - get average of last month - powerbi

How can get average of last month, I tried with this but it doesn't work out:
calculate (tablename[price],filter(tablename,max(tablename[date]))

Have you tried AVERAGEX?
Average Last Month =
AVERAGEX (
FILTER ( tablename, tablename[date] = MAX ( tablename[date] ) ),
tablename[price]
)

You can use:
CALCULATE (
AVERAGE ( table[price] ),
PREVIOUSMONTH ( 'Date'[Date] )
)
Remember you need a Date table in your model in order to use time intelligence functions in DAX.

Related

Same column Date difference based on other field DAX POWER BI

I want to calculate the date difference for all values in Field1 based on Field2 for example datediff(3/1/2020-2/1/2020) based on Field2 (c-b).
I have tried several ways but no luck so far.
there is no minimum mentioned in the question, so i assume that you are looking for the difference of the previous stages.
Date Difference =
DATEDIFF (
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER (
ALL ( 'Table' ),
'Table'[Date] < EARLIER ( 'Table'[Date] )
&& EARLIER ( 'Table'[Field1] ) = 'Table'[Field1]
)
),
'Table'[Date],
DAY
)
You can obtain a solution by adding 2 calculated columns:
First Calculated Column:
MinDate =
CALCULATE (
MIN ( YourTable[Date] ),
ALLEXCEPT ( YourTable, YourTable[Field1] )
)
2nd Calculated Column:
Date_Difference =
DATEDIFF ( [MinDate], [Date], DAY )
End Result:

Rolling Year: Actual And Last Year

I have the following fields:
Year
Category
Maker
Month
Month Number
Sales Volume
Sales
Date
So, I have in my dash a filter for "Month Number" and "Year":
My goal is to create two new measure; first with the Rolling Year that need to sum 12 months, ending in the moment that the user select in the mencioned filters. For example if y select Year 2021 and Month 01. The Rolling Year need to sum the sales of a selected category since 2020-02 to 2021-01 (thats mean always 12 months since a pivot month).
For thesecond is exactly the same, a measure called Rolling Last Year, it need to be a rolling sum too, but for the last period in order to compare. Taking the same example if I have the period 2020-02 to 2021-01. The Rolling Last Year for the last period should be 2019-02 to 2020-01.
I tried with this DAX code, that extracted from Microsoft page but without success:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL (Table[Date] ),
AND (
Table[Date] <= MAX ( Table[Date] ),
DATEADD ( Table[Date], 12, MONTH ) > MAX ( Table[Date] ))))
I share you an extract of my base:
Thanks in advance !!!
Based on the table and code you have shared, it is unclear from where the date filters are being applied. In case you have not done it, I strongly suggest to delete the [Month] and [Month Number] field from your Sales table and keep them in a separate Calendar table, from where the filters should be selected. Then, a simple tweak on you current formula should do the trick:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL ('Calendar'[Date] ),
AND (
Table[Date] <= MAX ( 'Calendar'[Date] ),
DATEADD ( 'Calendar'[Date], 12, MONTH ) > MAX ( 'Calendar'[Date] ))))
However you can try with this variation for the code, a little bit optimized so as not to scan your whole Sales table each time:
Rolling Year =
VAR EndSelectedDate = MAX ( 'Calendar'[Date] )
VAR StartSelectedDate =
CALCULATE (
MAX ( 'Calendar'[Date] ),
ALL ( 'Calendar'[Year] ),
'Calendar'[Year]
= MAX ( 'Calendar'[Year] ) - 1
)
RETURN
CALCULATE (
SUM ( Table[Sales] ),
ALL ( 'Calendar' ),
'Calendar'[Date] <= EndSelectedDate,
'Calendar'[Date] > StartSelectedDate
)

DAX formula for previous month's values

I'm new to DAX. In order to get the previous month's sales, I have used DATESBETWEEN and passed MAX(Date)-1 as the start date and as the end date EOMONTH.
I got the correct results. However, what are the other simpler ways of getting the same?
CALCULATE (
SUM ( AW_Sales[Sales Amount] ),
DATESBETWEEN (
AW_Calender_Lookup[Date],
DATE ( YEAR ( MAX ( AW_Calender_Lookup[Date] ) ), MONTH ( MAX ( AW_Calender_Lookup[Date] ) ) - 1, 01 ),
EOMONTH (
DATE ( YEAR ( MAX ( AW_Calender_Lookup[Date] ) ), MONTH ( MAX ( AW_Calender_Lookup[Date] ) ) - 1, 01 ),
0
)
)
)
update - based on an answer given I added a new measure, but the value is empty.
If you have a Dates table which is basically a calendar table, there is a simple way. Your data/fact table has to be connected to the Dates table using the date column for say date. Now you can create your Date slicer from the Dates table. For say, you selected the month September-2020 from the slicer, this below measure will return the SUM for current month-
total_sales = SUM(table_name[sales])
And this below measure will return the total sales for previous month-
total_sales_previous_month =
CALCULATE(
[total_sales],
PREVIOUSMONTH('Dates'[date])
)
For more information on DAX function PREVIOUSMONTH, you can check this.

DAX - TOPN() in a SUMX() for multiple transactions

I need your help.
I am trying to make a calculated column which sum the sales for the last two dates (there are not sales on all dates!) for each product.
I have tried to use the calculation:
CALCULATE (
SUMX ( TOPN ( 2; 'Table'; 'Table'[date_ID] ); 'Table'[Sale] );
ALLEXCEPT ( Table; 'Table'[Product_ID] )
)
But this only works if you have one sale per date per product ID.
What do I do if there are many transactions per date and prod ID?
Below I have a table where I have (filtered) one date and one product.
Now, I have made a calculated column that sums all transactions per date and product ID (for reference).
This is
Calculated Column 2 =
CALCULATE (
SUMX ( Table; Table[Sale] );
ALLEXCEPT ( Table; 'Table'[Date]; 'Table'[Product_ID] )
)
Now comes the difficult part.
If I want to sum the transactions for the last two dates on each product, I have made the following calculation:
Calculated Column 1 =
VAR SumProd =
CALCULATE (
SUMX ( Table; Table[Sale] );
ALLEXCEPT ( Table; Table[Date]; Table[Product_ID] )
)
RETURN
CALCULATE (
SUMX ( TOPN ( 2; 'Table'; 'Table'[Date] ); SumProd );
ALLEXCEPT ( Table; Table[Product_ID]; Table[Date] )
)
The Problems:
The calculations sums ALL the values in each category
Example: in the table, you see "Calculated column 1"=7571200, which is 27040 * number of transactions? I only want the value 27040.
For some reason, the TOPN() doesn’t work. If I change the N_Value=2 to N_Value=3, the calculation doesn’t change?
Please, does anyone know what is wrong with my calculation?
Thanks.
Br,
Jakob
In your measure, SumProd is a constant that you're summing for each row in the table. This is not what you want.
I'd suggest something more like this:
Calculated Column 1 =
VAR LastTwoDates =
CALCULATETABLE (
VALUES ( Table[Date] );
TOPN ( 2; ALLEXCEPT ( Table; Table[Product_ID] ); Table[Date] )
)
RETURN
CALCULATE (
SUM ( Table[Sale] );
ALLEXCEPT ( Table; Table[Product_ID] );
Table[Date] IN LastTwoDates
)
In this, we first calculate a list of the top 2 dates associated with Product_ID and store it as a variable. Then we use that as a filter when calculating the sum of Sale.
I'm not positive this exact syntax will work, but I hope it will point you in a better direction.
It turns out my code didn't work exactly as intended. Try this instead:
Calculated Column 1 =
VAR ProductDates =
CALCULATETABLE (
VALUES ( Table1[Date] );
ALLEXCEPT ( Table1; Table1[Product_ID] )
)
VAR LastTwoDates = TOPN ( 2; ProductDates; [Date] )
RETURN
CALCULATE (
SUM ( Table1[Sale] );
ALLEXCEPT ( Table1; Table1[Product_ID] );
Table1[Date] IN LastTwoDates
)

Total Working Days Calculation in PowerBi

I have the that has some data related to employee start date and end date for specific task
How to calculate "Total Working days" using the above data in PowerBi
The expected ouput would be number of days this employee worked (according to date )
so for example
12/2/2018 12/3/2018 = 1 working Day
but if same record repeated this would remain one working day
Output from Formula
Screenshot of PBX File
You can calculate the working day in various ways, I'll give you two options (both are calculated columns):
WorkingDays1 = Tasks[EndTime] - Tasks[StartTime]
(Assuming it's a date column type), you can add .DATE to be sure behind the column)
OR
WorkingDays2 = DATEDIFF(Tasks[StartTime];Tasks[EndTime];DAY)
If you want to correct the amount, for the same day, you can use an IF statement:
WorkingDays3 =
IF (
Tasks[StartTime] = Tasks[EndTime];
1;
DATEDIFF ( Tasks[StartTime]; Tasks[EndTime]; DAY )
)
If you wish to SUM the days, in a measure, you can SUM one of the generated columns above, OR you can create a measure which doesn't need such column: It iterates over the rows, and caculates the correct amount:
SUM WorkingDay =
SUMX (
Tasks;
IF (
Tasks[StartTime] = Tasks[EndTime];
1;
DATEDIFF ( Tasks[StartTime]; Tasks[EndTime]; DAY )
)
)
Result:
Note: These calculations do not take the weekends, or holidays into account.
Edit:
If you only want a unique list of dates, with the working days, you can use something like this. It itarates over the start dates:
SUM WorkingDay2 =
SUMX (
VALUES ( Tasks[StartTime] );
IF (
DATEDIFF ( Tasks[StartTime]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY ) = 0;
1;
DATEDIFF ( Tasks[StartTime]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY )
)
)
Edit 2:
If you want to create an unique list per day, you'll have to get rid of the time part in the datetime column. There are several ways to do this, but one option is to create a new column:
StartDate = Tasks[StartTime].[Date]
Then, you can adjust the measure by iteration over the dates, instead of the datetimes:
SUM WorkingDay3 =
SUMX (
VALUES ( Tasks[StartDate] );
IF (
DATEDIFF ( Tasks[StartDate]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY ) = 0;
1;
DATEDIFF ( Tasks[StartDate]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY )
)
)