DAX Looping Variable - powerbi

I'm building a Power BI dashboard that includes actual cost vs budget. Summarizing the cost is not a problem. The budget amount is entered in a table as a weekly amount, for example $1,000. We use a custom accounting calendar that dictates the number of weeks per month. The second month of each quarter is 5 weeks while all other months are 4 weeks. Here's what it looks like:
January is 4 weeks
February is 5 weeks
March is 4 weeks
April is 4 weeks
May is 5 weeks
June is 4 weeks
July is 4 weeks
August is 5 weeks
September is 4 weeks
October is 4 weeks
November is 5 weeks
December is 4 weeks
I need a looping variable in DAX that will simply count the number of weeks based on the months chosen with the month slicer. Then I can multiply the number of weeks by the weekly budget amount.
Here's a practical example:
If I choose Jan, Feb, Mar, Apr then the number of weeks should be 17 weeks. And the budget amount should be $17,000.
I figured this out in Crystal Reports. Here's the formula I wrote:
local datetimevar startdate:= {?BegMonth};
local numbervar numofWeeks;
While startdate <= {?EndMonth}
Do(
if month(startdate) in [2,5,8,11] then
numofWeeks := numofWeeks + 5
else
numofWeeks := numofWeeks + 4;
startdate := DATEADD("m",1,startdate);
);
numofWeeks
I've tried several approaches with DAX but I got nothing to work.

DAX doesn't exactly do looping. It does aggregations on filtered columns.
If you don't have a Month column, then create a calculated column.
TableName[Month] = MONTH ( TableName[Date] )
Once you have that, adding up weeks can be done with an iterator function.
Weeks =
SUMX (
VALUES ( TableName[Month] ),
IF ( TableName[Month] IN { 2, 5, 8, 11 }, 5, 4 )
)
The VALUES function is a list of unique values in the filter context, so if you selected Jan - Apr, then it would be {1,2,3,4}. Then SUMX iterates ("loops") over those four values and add 5 or 4 depending on the condition specified in the IF function.

Related

Sum up revenue for the last twelve months up until the first day of current month

I would like to extract a sum of revenue.
The sum should contain revenue for the twelve months passed up until the beginning of current month.
Following that, the sum of revenue will change only when another full month has passed.
I have browsed around a number of suggestions but they don't give me the required data. The last one gives me all revenue up until June 1, 2021 (i.e. twelve months prior to this month). And by all revenue, I mean ALL REVENUE generated prior to that date. Not just 12 months worth, but years...
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 12, 0) AS Period, SUM(SalesAmt) AS Sales, SalesRep, CurrencyTypeID
FROM dbo.SalesData
GROUP BY SalesRep, CurrencyTypeID, SalesID

DATEADD to calculate PYTD (Jan 1 till today's date) in DAX Power BI

I have a daily sales data for 3-4 years. I want to create the YTD and Prior Year To Date sales measure that will be updated daily. That is it should always be from beginning of the year (selected) to TODAY or the last day of the data (1 day lag from today and max date).
I used Sameperiodlastyear but it is problematic at the beginning of the month as it compares say Jan 1, 2022-June 8 2022 with Jan 1, 2021 with June 30, 2021.
Any suggestion how I can create a modified prior year to date measure to address this nuance?
This is a standard solution for the case. First, you get all dates, with a DATESYTD() function, for the current year or last visible year up today or last visible day, then you offset it.
SAMEPERIODLASTYEAR(DATESYTD(‘Date’[Date]))
It is equal to
DATEADD(DATESYTD(‘Date’[Date]),-1,YEAR))
Try this if you want to get exact days set:
VAR FirstDayThisYear =
SAMEPERIODLASTYEAR(STARTOFYEAR(‘Date’[Date])
VAR LastDayThisYear =
SAMEPERIODLASTYEAR(
LASTDATE(‘Date’[Date])
)
VAR SetOfDates=
DATESBETWEEN(
‘Date’[Date]
,FirstDayThisYear
,LastDayThisYear
)
RETURN
SetOfDates

Power Bi DAX: Total for the month

I need to make a total for each of the months based on the working days.
So far i have working days set as 1 if the day in the month is during Monday - Friday and 0 for Saturday and Sunday. I now need to total up the 1's and make it so that it is a single value for the month.
E.g Going the weekdays is 1 and weekend is 0, January will have 22 days on each row on the table in the data mode - January 1 = 232 January 2 = 22 etc. so i can use it to divide against my target which is set to the 1st of every month.
How can i do this??
try this. i haven't tested it.
=GROUPBY( FILTER(table, table[Working_Day]=1) , table[Month], ["new col", SUMX( CURRENTGROUP(), table[Month_Order])])
filter gives working day 1's data as table,then group by performed by month, then month order has been added and returns table.

Calculated Column in Power BI - Last 7 Days

I have a table in Power BI with the date and I am trying to create a calculated column that indicates if that date is within the last 7 days. I understand the syntax of the IF statement, but am having trouble with the actual time calculation. Can anyone assist?
Use the DATEDIFF function to reliably determine the number of days (or weeks, or months, or years) between two dates:
IF(
DATEDIFF(MyTable[MyDate], TODAY(), DAY) < 7,
"Within last 7 days",
"Not within last 7 days"
)

Drilling down periodical time data (subscriptions with start & end date) per month?

I'm currently struggling with following request in PowerBI:
I have two CSV files as PowerBI queries, one which defines fiscal months, and another one which lists all subscriptions including start and end date:
Fiscal month CSV:
Month Fiscal Start Fiscal End
January 03.01.2016 04.02.2016
February 05.02.2016 03.03.2016
March 04.03.2016 06.04.2016
April 07.04.2016 02.05.2016
May 03.05.2016 06.06.2016
June 07.06.2016 03.07.2016
July 04.07.2016 05.08.2016
August 06.08.2016 02.09.2016
Subscription CSV:
Account-ID Subscription-Start Subscription-End Item Count
101 08.01.2016 07.02.2016 5
102 15.01.2016 14.03.2016 3
103 05.01.2016 04.06.2016 10
101 08.02.2016 07.03.2016 3
104 10.04.2016 09.05.2016 5
105 16.04.2016 15.07.2016 2
My challenge now is to drill down all subscription item counts per fiscal month as a powerBI table.
Note: an Item Count is valid for a fiscal month if its Subscription-Start < Fiscal End and its Subscription-End > Fiscal End. (Example: A subscription from 15.01.2016 - 14.02.2016 should be counted in january, but not in february)
PowerBI table (schematical example):
Month Item Count
January 18
February 16
March 10
April 17
May 12
June 2
July 0
August 0
How can I implement this report in PowerBI?
THX in advance for your help and BR
bdriven
I've found following solution for my problem:
First I've created a new Table and made a crossjoin of the two queries. Then I've filtered for the lines, where my Subscription Start was before the Fiscal Month End and Subscription End was after the Fiscal Month End.
Based on this new table I can create all respective reports.
Example Code see below:
Fiscal Month Report =
FILTER(
CROSSJOIN(
ALL('Fiscal_month');
ALL('Subscription')
);
('Subscription'[Subscription-Start] < 'Fiscal_month'[Fiscal End] && 'Subscription'[Subscription-End] > 'Fiscal_month'[Fiscal End])
)