Power BI : why EARLIER() does not recognize date column? - powerbi

I am trying to make a cumulative measure in Power BI as Following :
prices = SUM(Testing[price])
comulativetest = CALCULATE([prices],FILTER(Testing,EARLIER(Testing[Date])>=Testing[Date]))
but this error message appear to me
and this is the table
what's the problem with it, please?

Earlier is mostly used in the context of "calculated columns". I think you are trying to use it in a measure and that's why you are getting an error.
Create a "Dates" table, and create a (one-to-many) join between that and your "Testing" table. Then use a measure like this:
comulativetest :=
CALCULATE (
[prices],
FILTER (
ALL ( Dates ),
Dates[Date] <= MAX ( Dates[Date] )
)
)

Related

How to count dates in Power BI DAX

I have a table like below.
I want to show count of LicenseEndDate. Like In my slicer I have taken WeekEnding column suppose if I select weekending date like 09/25/2022 then I want show the count of LicenseEndDate dates that are ended between 09/18/2022 to 09/25/2022. How to create measure for this. I want to show that count in card visual. I created a measure like below
License_Expired_Count = COUNT(Trade[LicenseEndDate]).
But It giving me count of all. That mean the License which are expiring in the feature count also it was showing. But want to show count of license which are expired in the selected period. How create measure for this.
Please check this code, let me know If It works for you.
License_Expired_Count =
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
Trade[LicenseEndDate] <= SELECTEDVALUE ( Trade[WeekEndingDate] ),
Trade[LicenseEndDate]
> SELECTEDVALUE ( Trade[WeekEndingDate] ) - 7
)
try this :
License_Expired_Count =
VAR _slt =
SELECTEDVALUE ( Trade[WeekEndingDate] )
RETURN
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
ALLSELECTED ( Trade[WeekEndingDate] ),
DATESBETWEEN ( Trade[LicenseEndDate], _slt - 7, _slt )
)
check sample file attached

DAX TREATAS filtering by another table to get sales of all products on promotion

How to filter all products on promotion? Say we have two tables Sales and Budget without physical relationship. Here model is simplified and let's assume that it is the case, we cannot create physical relationship. We have to use virtual relationship.
We can see summary:
The two first columns are of the Sales table. The third column BudgetTreats is a measure:
BudgetTreatas =
CALCULATE (
SUM ( Budget[amount] ),
TREATAS (
VALUES ( Sales[id] ),
Budget[id]
)
)
Now I would like to resolve two things:
How to make a slicer to filter out only the products (id) which have BudgetTreatas?
How to create a measure for calculating sales but only for products which have a budget? So analogous measure as BudgetTreatas presented above.
And of course sample data: DAX TREATS.pbix
I posted an answer to my question but it is not to show an answer but rather to show working solutions, and give you idea on expected results. I would be grateful for any answer or comments.
References:
The Logic behind the Magic of DAX Cross Table Filtering
Virtual Filters Using TREATAS
How To Use The TREATAS Function - Power BI & DAX
Creating Virtual Relationships Using TREATAS - Advanced Power BI Technique
Measure calculating Sales filtered by ids in Budget table.
Surprisingly this is not working:
//not working:
SalesFilteredByBudget1 =
CALCULATE (
[Sales],
TREATAS ( VALUES ( Budget[id] ), Sales[id] )
)
It seems we need an extra table. If we add to the model a Bridge table with all sales id and connect it to Sales table on id (without connecting it to Budget table!) we may resolve the issue.
//works:
SalesFilteredByBudget2 =
CALCULATE (
[Sales],
TREATAS ( VALUES ( Budget[id] ), Bridge[id] )
)
So it seems filters propagate further from tables used in TREATAS( VALUES on the tables connected by physical relations.
If we want to make a measure without Bridge table we can make extra table as a table variable.
// works:
SalesFilteredByBudget3 =
VAR Lineage =
TREATAS ( VALUES ( Budget[id] ), Sales[id] )
VAR tbl =
CALCULATETABLE ( Sales, KEEPFILTERS ( Lineage ) )
VAR result =
CALCULATE ( SUMX ( tbl, [amount] ) )
RETURN
result

Power BI - Running total column with many filters

I have table "Sales" (fields: Product, Country, Date, Sales) with monthly sales across many products and countries. Also I have tables with calendar, list of products, list of counties that are linked with this table. I want to add column to "Sales" with running total sales across each Product/Country, see the field with desired result "Running total".
I tried to use
YTD = TOTALYTD(SUM(Sales[Sales]); Calendar[Date]) but it didn't work. I think I need to use filters in TOTALYTD function, but I also didn't manage to understand how. Can you suggest to me a right solution to my case?
Table "Sales"
I was suggested to use this code
Column =
SUMX (
FILTER (
Sales,
Sales[Product] = EARLIER ( Sales[Product] )
&& Sales[Country] = EARLIER ( Sales[Country] )
&& Sales[Date] <= EARLIER ( Sales[Date] )
&& YEAR ( Sales[Date] ) = YEAR ( EARLIER ( Sales[Date] ) )
),
Sales[Sales]
)
It worked.
I partially coped with my issue by creating set of measures for each combination of product and country:
A_US = TOTALYTD(SUM(Sales[Sales]);'Calendar'[Date];FILTER(All(Sales);Sales[Product]="A"&&Sales[Country]="US"))
A_Canada = TOTALYTD(SUM(Sales[Sales]);'Calendar'[Date];FILTER(All(Sales);Sales[Product]="A"&&Sales[Country]="Canada"))
and so on. But what if i have 100 products and 30 countries? I think I need to create a column "Running total" in "Sales" that calculates running total for each product and aech country.
The problem of the TOTALYTD function is that it takes only one filter.
The tricks is to use the filter function like you do on the second response.
To use only one column for all product and country you have to get the context of the current row .
To achieve this you have the function earlier in dax.
Here the documentation about earlier : https://learn.microsoft.com/en-us/dax/earlier-function-dax
The column need to be build with this expression :
TOTALYTD(SUM(Sales[Sales]),'Calendar'[Date],filter(Sales,and(Sales[Country]=EARLIER(sales[Country]),sales[Product] = EARLIER(sales[Product]))))

Running Total with Slicer from another table

I have a running total dax measure that works. Problem now is that the slicer on the page is coming from another data set which is linked to the source table of running total data set and when you select the slicer it doesn't filter anything.
Homes Connected =
CALCULATE (
SUM ( refv_FTTH_HomesConnected[ActualHomesConnected] ),
FILTER ( ALL ( refv_FTTH_HomesConnected ), refv_FTTH_HomesConnected[Month_sort2] <= MAX ( refv_FTTH_HomesConnected[Month_sort2] ) )
)
Is there a way to incorporate the columns from the other dataset to get the desired result?
The ALL in your FILTER removes any slicer selection filtering.
Try using ALLSELECTED instead.

Cumulative total by group in Power BI (DAX)

After googling for two pages, I'm struggling to find a simple way to create a cumulative sum measure by date and item in Power BI (using DAX). I have a table which contains:
Username
Date (DD-MM-YYYY)
Number of requests in that day
I have managed to obtain the cumulative sum by using the following expression (extracted from DAXPatterns):
CALCULATE (
SUM ( Table[Requests] ),
FILTER (
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX ( 'Date'[Date] )
)
)
But I would like to obtain a measure indicating how many requests have been made by a user up to a certain date.
Is there a simple way to do this?
Create calculated table using SUMMARIZECOLUMNS function and apply filter that you need on the top of that calculated table.
YourCalcTableName =
SUMMARIZECOLUMNS (
'UsernameTable'[Username],
'Date'[Date],
"Number Of Requests", SUMX ( 'UsernameTable', 'UsernameTable'[NumberOfRequests] )
)
This calculated table essencialy produces 3 column table with user name, date and number of requests sent by this user on this date.
SUMMARIZECOLUMNS