What's wrong with this formula? ( while i'm making KPI) - powerbi

thisyearsales =
SUMX(
FILTER(
'Sales Transactions',
'Sales Transactions'[year]=2022
),
'Sales Transactions'[value]
)
lastyearsales =
SUMX(
FILTER(
'Sales Transactions',
'Sales Transactions'[year]=2021
),
'Sales Transactions'[value]
)
I don't understand why 'thisyearsales' cause error while 'lastyearsales' work right.
Related tables are below :
Value
Date
Year
10000
2021-01-01
2021
20000
2021-01-02
2021
10000
2021-01-03
2022

I can't reproduce your error. Both measures are working fine for me.
However, I don't see why you are using an iterator function SUMX() when there is nothing to iterate over, while you actually need a function that changes the filter context, like CALCULATE() does:
Last Year Sales =
CALCULATE(
SUM('Sales Transactions'[Value]),
'Sales Transactions'[Year] = 2021
)
This Year Sales =
CALCULATE(
SUM('Sales Transactions'[Value]),
'Sales Transactions'[Year] = 2022
)

Related

PowerBI / DAX - REMOVEFILTERS not working as expected

We are using a date table based on a fiscal calendar. I need to determine if a particular year has 53 weeks or 52 weeks.
The page has a slicer with a date hierarchy that is used to select a year, quarter, month, or week. I would like the Measure to return the maximum number of weeks in a particular year regardless of what is selected.
The measure below works fine, it returns 53 which is the maximum number of weeks in any year regardless of the selection in the date hierarchy slicer.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate))
However, when I try to add a filter condition to select the proper year it no longer works it returns the maximum week number of the period selected in the hierarchy. If I select January (a 5 week month) it returns 5 instead of 53 then number weeks in fiscal 2021.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate),filter(vwPBIDate,vwPBIDate[Fiscal Year Number]=2021))
You're missing an ALL:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
FILTER(
ALL( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)
)
which is equivalent to:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)

DAX previous year date calculation not showing correctly in Power BI measure

I have a measure that sums expense amount for the current filtered year. This works as desired.
Expense = CALCULATE(SUM('GL Data'[Amount]),FILTER('GL Data','GL Data'[SubGrp1] = "EXPENSE"))
I want to sum the expense amount for the same time period of prior year. My measure calculates correct data as of 11 months ago, not 1 year ago. It appears to function as if the data calculation is looking at my system date, not the specified date:
Expense PY = CALCULATE([Expense], DATEADD(Dates[Date],-1,YEAR))
I've attempted using -12, -13 and MONTH in the DATEADD but the result doesn't correctly total 1 year ago. I've also tried SAMEPERIODLASTYEAR() which provides wrong info. Perhaps my Expense measure is the culprit.
My Date table is generated with this Dax:
Could you please use this as your date table, and test it. In your date table, you imposed no restrictions on the calendar auto() functions which I think is not useful. Also You used Generate function which ignores rows with blank values. In such a case, use GENERATEALL() function; but You need to form your date table like this:
Date =
VAR MinYear =
YEAR ( MIN ( Fact_Table[Date_Column] ) )
VAR MaxYear =
YEAR ( MAX ( Fact_Table[Date_Column] ) )
RETURN
ADDCOLUMNS (
FILTER (
CALENDARAUTO ( 12 ),
AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
),
"Calendar Year", "CY " & YEAR ( [Date] ),
"Day", FORMAT ( [Date], "dddd" ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "mmmm" ),
"Year Month", FORMAT ( [Date], "mmm yy" ),
"Year", FORMAT ( [Date], "YYYY" )
)
After creating your Date Table, please do not forget to mark it as date table which is a good DAX practice as in the photo below.

Sector and Geography granularity issue

I have a table Time
When the user selects period from february 2020 tç february 2021,I expect to get the following result
But what I get
How to correct the issue with the granularity?
Here is the pbix file https://drive.google.com/file/d/1vf0khr7MbnDTVBzycNgJsaJufwd9F8D_/view?usp=drivesdk
Try this below Measure-
project_wise_time =
SUMX(
SUMMARIZE(
'Table (2)',
'Table (2)'[Project Name],'Table (2)'[DATE],
"distinct_value", DISTINCT('Table (2)'[Time ])
),
[distinct_value]
)
Here is the output-

Calculate Current and Previous month's Value based on slicer selection in power bi

I have two tables called
Main Table contains the following data(Pic) and
Date table created using the dates from the Main table.
I want to calculate two fields/measures based on Date slicer selection from the Date Table
current month's revenue
previous month's revenue
Example: If I selected the 4th Month then it should sum distinct revenue of client A and B for the 4th month as current_month_revenue and Sum distinct revenue of A and B for 3rd month as previous_month_revenue.
I tried writing the following Measure to Calculate current_month_revenue and it is working fine but it is not giving the correct result for Previous_month_revenue.
I am getting the same value for the Previous month as well.
'Measure Previous Month Revenue' =
IF (
ISFILTERED ( 'Date'[Year_Month] ),
VAR myTable =
SUMMARIZE (
'Main Table',
'Main Table'[ClientName],
'Main Table'[Mon],
'Main Table'[Revenue]
)
RETURN
CALCULATE (
SUMX (
myTable,
'Main Table'[Revenue]
),
FILTER (
'Main Table',
'Main Table'[Mon]
= SELECTEDVALUE ( 'Date'[Month] - 1 )
)
),
VAR myTable =
SUMMARIZE (
'Main Table',
'Main Table'[Revenue],
'Main Table'[Mon],
'Main Table'[Revenue]
)
RETURN
SUMX (
FILTER (
myTable,
'Main Table'[Mon]
= MONTH (
TODAY ()
) - 1
),
'Main Table'[Revenue]
)
)
Desired Output
If 4th month is selected
Current Moth revenue = 100 + 200 = 300
Previous Month = 100+200 = 300
In this case, both are the same but in actual data, revenue is different for each month.
I see from the code that the 'Date' table has a numeric column called Month, that I assume to be of the same type as the Mon column in your 'Main Table'.
Also, since in the 'Main Table' there is no Year, I assume that the Year is not to be considered.
From the slicer over the Date table we can directly get the selected 'Date'[Month] using SELECTEDVALUE(). As default parameter we use the current month obtained by the TODAY() function.
Then we obtain the Previous Month subtracting one from the Selected Month and we can use it to slice the table grouped by CustomerName, Mon and Revenue. Grouping is needed to remove duplicate Revenues for the same customer on the same month and is implemented using SUMMARIZE()
As a final step we can aggregate the 'Main Table'[Revenue] of the filtered and grouped table using SUMX.
'Measure Previous Month Revenue' =
VAR CurrentMonth =
MONTH(
TODAY()
)
VAR SelectedMonth =
SELECTEDVALUE(
'Date'[Month],
CurrentMonth
)
VAR PrevMonth = SelectedMonth - 1
VAR MyTable =
CALCULATETABLE(
SUMMARIZE(
'Main Table',
'Main Table'[ClientName],
'Main Table'[Mon],
'Main Table'[Revenue]
),
'Main Table'[Mon] = PrevMonth,
REMOVEFILTERS( 'Date' )
)
VAR Result =
SUMX(
MyTable,
'Main Table'[Revenue]
)
RETURN
Result
The same code but for the Previus Month calculation can be written for the current month
'Measure Current Month Revenue' =
VAR CurrentMonth =
MONTH(
TODAY()
)
VAR SelectedMonth =
SELECTEDVALUE(
'Date'[Month],
CurrentMonth
)
VAR MyTable =
CALCULATETABLE(
SUMMARIZE(
'Main Table',
'Main Table'[ClientName],
'Main Table'[Mon],
'Main Table'[Revenue]
),
'Main Table'[Mon] = SelectedMonth,
REMOVEFILTERS( 'Date' )
)
VAR Result =
SUMX(
MyTable,
'Main Table'[Revenue]
)
RETURN
Result
A better solution could be implemented setting a relationship between the 'Date' table and 'Main Table'.
Depending on the business requirement, it could be possilbe to use a Date table at the month level granularity, with a YearMonth column instead of Mon, or at the Day level, with a Date column instead of the Mon column.
CALCULATE does not affect variables that you've already defined, so FILTER does nothing to the first SUMX. See this related post for a bit more detail.
I'd suggest writing the measure much more simply. Something like this:
Previous Month Revenue =
VAR PrevMonth =
SELECTEDVALUE (
'Date'[Month],
MONTH ( TODAY () )
) - 1
RETURN
CALCULATE (
SUM ( 'Main Table'[Revenue] ),
'Main Table'[Mon] = PrevMonth
)

How to generate calendar table for each calendar month and make it stop as of today in Power BI

I am able to generate calendar table for each month, but it goes till the end of current month.
CalendarTest =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR ("01-01-2018", TODAY()),
"Month", EOMONTH([Date], - 1) + 1
),
[Month],
"Eomonth",EOMONTH([Month],0)
But the last value for Eomonth should be today's date. Not the end of the month.
Need it to be like that:
Try
CalendarTest =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR ("01-01-2018", TODAY() ),
"Month", EOMONTH ( [Date], - 1) + 1
),
[Month],
"Eomonth", MIN ( EOMONTH([Month],0), TODAY() )
)