Cumulative number of entries in dates range - powerbi

I'm trying to get cumulative number of unique IDs in given timeframe.
My DAX look like this:
Cumulative = CALCULATE(SUM(Data[ID]));DATESBETWEEN(Data[ack_date];DATE(YEAR(NOW());4;1);DATE(YEAR(NOW());11;30)))
There is similar measure for Year-1: [YEAR(NOW())-1]
What I want to achieve is area chart showing growing number od IDs in time comparing same periods this and previous year. When I give those measures as Values for chart and "ack_date" as its Axis what I get is values comparison month by month but not cumulative, just value for certain month.

Try this code. Adjust for year -1.
=
CALCULATE (
DISTINCTCOUNT ( Data[ID] ),
FILTER (
ALL ( Data ),
AND (
Data[Ack_date] <= MAX ( Data[Ack_date] ),
AND (
Data[Ack_date] <= DATE ( YEAR ( NOW () ), 11, 30 ),
Data[Ack_date] >= DATE ( YEAR ( NOW () ), 4, 1 )
)
)
)
)

Related

Power BI, Line Chart with Base Value 100

I need to build a line chart that is supposed to show the cumulative increase from Day 1 to the End.
An Example would be like this:
Date Capital Value
31-Jan 237 100.00
28-Feb 250 105.48
31-Mar 210 88.60
30-Apr 300 126.58
In other words, is dividing every value by the first value, not of the whole table but the dates that are been displayed in the chart. The first date is dynamically changed as time passes, making it impossible to fix the formula to 31-Jan for example
Kind Regards
Try this measure:
% Capital running total in Date =
VAR runtotal =
CALCULATE (
SUM ( 'YourTable'[Capital] ),
FILTER (
ALLSELECTED ( 'YourTable'[Date] ),
ISONORAFTER ( 'YourTable'[Date], MAX ( 'YourTable'[Date] ), DESC )
)
)
VAR baseval =
CALCULATE (
SUM ( 'YourTable'[Capital] ),
FIRSTDATE ( ALL ( YourTable[Date] ) )
)
RETURN
DIVIDE ( runtotal, baseval )
Note that for calculating the Running Total Power BI can help you with a Quick Measure.

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.

I will like a script in power bi that will calculate percentage increase or decrease from one month to the previous month

I want to display percentage increase or decrease in total for each month as I select each month i.e when I click on FEB, it should tell me whether there was a percentage increase/decrease in expenses compared to JAN.
I have tried different codes but keep getting an error message.
Here is a DAX CODE I tried:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
population[MONTH]
= ( EARLIER ( population[MONTH] ) - 1 )
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH ),
0,
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH
I want a new column created to display the percentage increase or decrease from a month to its previous month.
Here is a screenshot of the excel document:
The Column 'Month' is not of type date. How would PowerBi know the text APR represents April? You need to make this column a date.
Now you need to change the script to work with DateDiff:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
DATEDIFF(population[MONTH], EARLIER ( population[MONTH] ),MONTH) = 1
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH );
0;
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH)

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 )
)
)