Add leading 0 (zero) to month number in power BI - powerbi

I have the below DAX formula that is concatenating a month number to a year.
If the month number is less than 10, i want to add a leading zero to it but i'm new to DAX and i can't seem to figure out how to do it.
Expiry_MonthYear_Sorter = [Expiry_Date].[Year] & [Expiry_Date].[MonthNo]
As an example, if the year is 2018 and the month number is 2, i want the value to be 201802.
Currently, my formula gives me 20182

You can use the FORMAT function for this.
Expiry_MonthYear_Sorter = FORMAT([Expiry_Date], "YYYYMM")

What you are looking for in DAX is an IF statement.
Assuming this is part of a calculated column in your dataset, then something like below is what you are looking for. Where if your month number is less then 10, then append a 0 between year and month, else just append year and month.
Expiry_MonthYear_Sorter = IF ([Expiry_Date].[MonthNo] < 10 ),
[Expiry_Date].[Year] & "0" &[Expiry_Date].[MonthNo],
[Expiry_Date].[Year] & [Expiry_Date].[MonthNo]
)

Related

Power BI Measure to return date if the condition is true

I am struggling in writing a measure which returns me the list of dates if the condition is true.
For example, Last 7 days, I was able to do it in calculated column as below
Last 7 days = if(DateTime[Datetime]<=TODAY(),DateTime[DateTime])
The problem with the calculated column is when I am using this column as a filter, It is showing the dates that are not the last 7 days as blanks which I dont want. Please help.
Since you want to return a list of dates you need a Calculated Table:
Last 7 days =
FILTER(
'Datetime',
AND(
'Datetime'[Datetime] > TODAY() - 7,
'Datetime'[Datetime] <= TODAY()
)
)
You can use this table to filter your other data.

How to get week number per month on Power Bi?

I have a a column called Date originated that hold the a date like "Tuesday, March 21, 2022".
I want to get that week number per month but the following gives me incorrect numbers.
The following DAX: Week of Month = 1 + WEEKNUM('ECR'[Date Originated]) - WEEKNUM(STARTOFMONTH('ECR'[Date Originated]))
gives me 1 for that date.
Some entries are correct like for Wednesday, April 6, 2022 I do get 1
The problem is not the DAX expression, but the date column, which you didn't share with us.
STARTOFMONTH is not the right choice of function here, since that function does not by default return the first date of a given month; rather, it returns the first date of the month for the supplied dates (within the current context).
Here you are supplying only one date, and so, for example:
=STARTOFMONTH(21/03/2022)
will simply return 21/03/2022.
You should be using
=
1 + WEEKNUM( 'ECR'[Date Originated] )
- WEEKNUM( EOMONTH( 'ECR'[Date Originated], -1 ) + 1 )

DAX: How to count how many months have sales in a period

In my fact table (fTable) the columns I have are dates, region and sales.
dates
region
sales
-----
------
-----
I am visualizing the data in a pivot table with regions as rows and months as columns (I have a date table (dDate) with a months column in my model)
I am looking for a way to dynamically change the denominator in an averaging measure if a certain region doesn't have sales in a given month. Right now my denominator is hard-coded as 6, because I am averaging 6 variables in my nominator, but any one of them could be 0 if I don't have any sales in a certain month, in which case my denominator needs to change to 5, 4 or less depending on how many months I don't have sales in. So I am looking to count how many of the past 6 months have sales and sum that as the denominator.
I have managed to count months with sales this way:
Denominator:=
var newTable = Summarize(fTable,fTable[date (month)], fTable[region],"Sales",[Sum of Sales])
var MonthsWithSales = Countrows(newTable)
RETURN
MonthsWithSales
I've tried to RETURN
Calculate(SUMX(newTable,MonthsWithSales), Dateadd(dDate[Date],-6,MONTH)
but it yields a wrong result.
Any suggestions?
Thanks
Based on my sample, we can use function VALUES & COUNTROWS inside CALCULATE to get what we need:
Measure = CALCULATE( COUNTROWS(VALUES('Table (2)'[Month])), ALL('Table (2)'[Month]) )

Calculated Column for first 5 working days in each month

I wanted to do calculated column in my calendar table in Power BI. The calculated column should show "1" for the first 5 working days in each month in the calendar table, the rest of the days should be "0" . I tried to come out with the formula shown below
tick = CALCULATE(COUNT('Calendar'[Weekend - weekday]), 'Calendar'[IsWorkingDay] = TRUE)
But it shows "1" for all the working days but the desire output is the first 5 working days of each month. Anyone could help me
Suppose this is your original table with new column for Weekday-weekend, you can calculate the new column to display 0 for first five working days using rankx & If, followed by 0 and blank for other case, here is the dax formula:
Remark: Both 1 and 0 need to with quotation mark, as using with "" without convert to string will cause Data is variant type error.
tick1 =
var rank1 = RANKX(FILTER(Sheet1,Sheet1[Weekday-Weekend] = "Weekday" && Sheet1[Period] = EARLIER(Sheet1[Period]) ),Sheet1[Day],,ASC)
return
IF(Sheet1[Weekday-Weekend] = "Weekday" && rank1 >=1 && rank1 <=5, "1",
IF(Sheet1[Weekday-Weekend] = "Weekday", "0",""))
The table with add column

Calculate average of data at different levels by year

I have the following Data :
I want to achieve a solution where, when I filter year, it returns me the average of Goodwill of that year and the previous year.
So for my year filter 2017: Average of Goodwill in 2017,2016
year filter 2016: Average of Goodwill in 2016,2015
.... and so on
The year is in General format (NOT Date format) ..
Expected OUTPUT Values:
Not sure what you have tried (or how familiar you are with Power BI/DAX), but you will most likely need to use a combination of DIVIDE, CALCULATE, and ALL. The DIVIDE function isn't super necessary, but it is a great habit to get into as it catches errors. Combining the CALCULATE and ALL will allow you to take the sum for the selected year and the prior year.
What I would do is this:
Goodwill - 2yr Avg =
VAR MaxSelectedYear = MAX([Year])
VAR PriorYear = MaxSelectedYear - 1
VAR MinYearInData = MINX(ALL(Data), [Year])
RETURN
DIVIDE(
CALCULATE(SUM([Goodwill]), FILTER(ALL(Data[Year]), [Year] = MaxSelectedYear))
+
CALCULATE(SUM([Goodwill]), FILTER(ALL(Data[Year]), [Year] = PriorYear))
,
IF(PriorYear < MinYearInData, 1, 2),
BLANK()
)
The IF statement at the end will catch when you are looking at the first year, so that the sum is only divided by 1 instead of 2. The sum will only be 1 year of data since when you filter the Data table to the prior year, there will be no data.
Maybe something like this?
My Average =
IF(HASONEVALUE(Data[Year]);
DIVIDE(
CALCULATE(SUM(Data[Goodwill]);FILTER(ALL(Data[Year]);Data[Year]>=SELECTEDVALUE(Data[Year])-1 && Data[Year]<=SELECTEDVALUE(Data[Year])))
;2);
BLANK()
)
HASONEVALUE, just to identify if my current scope only has 1 year selected for the calculation to be applied.
Than getting the value for the current year and previous year with FILTER and SELECTEDVALUE
I am hardcoding the division by two, but you can adjust it depending on your dataset and purpose.