I am writing code to determine the Running Total Customers: All Customers Until This Period. Now, I have created a This period's Customers table to contain the following code:
PeriodeKlant = DISTINCTCOUNT(Text[PCHN])
I have now created the code as below:
Running Total Customers =
var _currdate=MAX(Tekst[Datum].[Date])
var _salesthisperiod=Tekst[Verkoopdoc]
return
if(_salesthisperiod>0,
CALCULATE(
[PeriodeKlant],
FILTER(
ALLSELECTED(Tekst[Datum].[Date]),
ISONORAFTER(Tekst[Datum].[Date], _currdate, DESC)
)
)
)
I get the message that the previously created column cannot be used, this column has the value integer and at summary it says sum. I don't know if that is why I would not be able to load this data?
But because of this error I can't run my measure.
The way your DAX is written, it's expecting [PeriodeKlant] to be a measure rather than a calculated column. Try deleting this calculating this column and defining it as a measure instead.
I have a dataset where I am trying to calculate the user response time to dealer note using the note date of every action.
Here is my sample data look like, I've calculated this in hive query using lag, lead, and min window functions, but my user wants to see this in Power BI.
This is what I tried so far.
I've created a "user note date" measure to get the first response of the User response
user note date = CALCULATE(MIN(Query1[Note Date]),ALLEXCEPT(Query1,Query1[incident],Query1[Action Type]),
LEFT(Query1[lastuser],1) in {"U"} )
Dealer Note Date =
CALCULATE(
MIN(Query1[pdate]),
FILTER(ALLEXCEPT(Query1,Query1[incident],Query1[action_type]),
Query1[action_type] in {
"DLR_CUST_Update"
))
I am getting this error from Dealer Note Date Measure, I am not understanding what's wrong with the above calculation.
error: A single value for column 'Action Type' in table 'Query1' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min
Here is my sample data
Your column in calculation for [Dealer Note Date] is query1[action_type] or Query1[Action Type]??
You cant access column Query1[action_type] in [Dealer Note Date], because you are excluding it in ALLEXCEPT
Dealer Note Date =
CALCULATE(
MIN(Query1[pdate]),
FILTER(ALLEXCEPT(Query1,Query1[incident],**Query1[action_type]**),
Query1[action_type] in {
"DLR_CUST_Update"
))
I currently have a raw data table called Score Table which has three columns: (1) an entity ID, (2) a score, and (3) a date.
I also have a Measure for a single dynamic date (I can't use date slicers for this for reasons that I won't bore you with).
My goal is to have a measure tell me what the last score is before the date indicated by the Measure.
I for some reason am unable to pull this off in DAX. Whenever I try to use Calculate( functions in DAX, I end up only pulling the final date in the 'Score Table' [Date] column (not before the Measure date and not different by ID.
Any help on this would be greatly appreciated.
I still miss some information to give you a specific answer, but something like this should work.
PreviousScore =
CALCULATE(
LASTNONBLANK('Table'[Score])
,FILTER(
ALL('Calendar')
,'Calendar'[Date] < [YourDateMeasure]
)
)
Note that I'm assuming you have a Calendar table to manage the dates (you usually want one of those in your model)
I need to create a calculated column that is based on another column but depends on the date filter the report is run for.
If the item is own for more than a year it is 'Comparable' if less than a year it is 'Non Comparable'.
I have Item, DateOfPurchase in T1 and Date in T2 (Period table)
I have come up with DAX using today() but it only works if we report on today's date.
This didn't work (no idea why)
=if( dateadd( 'Item'[PurchaseDate],1,year)<today(),"Comp","Non-Comp")
This worked but only for current period
=DATEDIFF('Item'[PurchaseDate],today(),MONTH)
= if('Item'[DateDiff]>12,"Comp","NonComp")
However, I can not use that column when running report for a different period, because attribute is not valid for prior periods.
Since calculated columns are computed only once, when the table is processed/refreshed, you cannot use a calculated column for your scenario.
Instead, consider using a disconnected parameter table. In your case, this would be a table with 1 column and just 2 rows: "Comp" and "NonComp". You can create this as a calculated table like so:
ParameterTable = DATETABLE("Value", STRING, {{"Comp", "NonComp"}})
Based on what the user selects on this table - which you can find using SELECTEDVALUE(ParameterTable[Value]) - you apply the relevant logic in a measure instead:
BaseMeasure =
// Whatever you are trying to calculate
SUM(Item[Amount])
Measure =
// This measure will respect the user selection (Comp / NonComp) and the current period:
VAR compValue = SELECTEDVALUE(ParameterTable[Value])
VAR today = MAX('Date'[Period])
RETURN
SWITCH(
compValue,
"Comp", CALCULATE( [BaseMeasure] , DATEDIFF('Item'[PurchaseDate], today, MONTH) > 12),
"NonComp", CALCULATE( [BaseMeasure] , DATEDIFF('Item'[PurchaseDate], today, MONTH) < 12),
[BaseMeasure] // Fallback, in case user didn't select Comp/NonComp
)
If you have multiple base measures in your report, you will need to implement this pattern for each of your base measures.
I've got two primary datasets:
Real data with all sales transactions.
For example RealData:
Date;Sales
16-01-2017;1200
20-01-2017;1500
05-02-2017;800
08-02-2017;1100
13-02-2017;300
Etc.
A plan with total sales I want to achieve, with totals at the last day of each month.
For example EndOfMonthTargets:
Date;Sales
31-01-2017;3000
28-02-2017;2500
Etc.
In my real data I (a) also have data from the years before 2017, and obviously I have the targets for each month in 2017. The RealData is always live / up to date.
What I'm trying to do is show KPI visualizations: how is the real data vs the plan doing. However, I can't figure out how to relate the two datasets, because the cannot really be joined together.
What is the idiomatic way to do this in PowerBI?
I've tried:
adding a column to each source with its respective status ("real" vs "plan")
creating a union (i.e. "Append Queries as New")
adding a column Month = MONTH([Date])
Giving me this data:
But that's weird, because then the KPI visualization will include the "Plan" data either at the start or worse at the end where it will be plotted as the "current" number.
The expected output is a KPI visualization:
showing the absolute number of sales summed by year (or month, if my slicers are set that way!)
with the Target goals retrieved from the plan data
with a trend based on previous years (not included in my sample data)
How do I achieve that?
There are a few ways to join tables like this. What you have are 2 tables with different granularities: a sales table that goes to the day, and a target table that goes down to the month. It's a common request to model them against each other, and it boils down to having a lookup table that both fact tables can join with.
What I would recommend:
Have a table of your real data
Have a table of your plan/target data, using the last day of the month is fine
Do not relate these 2 fact tables together directly
Rather, have a separate date lookup table, to which both tables are joined. This date dimension should, at a minimum, have a month & year column (e.g. March 2017).
Join both fact tables to your date table
Hide the date fields in your 2 fact tables, so that you aren't tempted to use them in visuals (you have to use the date fields from the lookup table instead)
You can then create measures to SUM your actual and targets. And then additional measures that subtract those 2 measures against each other (or divide one into the other). All those measures will then be sliceable by the month & year on your date table. (You could slice them by date too, but because targets are assigned to the last day of the month, that granularity will be less than helpful.)
There's a lot of good information & examples here: http://www.daxpatterns.com/handling-different-granularities/
The way I would do it:
Add a calculated column to both real and targets tables:
Month = Date(Actual[Date].[Year],Actual[Date].[MonthNo],1)
Month = Date(Target[Date].[Year],Target[Date].[MonthNo],1)
Create relashionships between these
Plot SUM(RealData[Sales]) and Target[Sales] against Target[Month]
You can use PowerQuery to transform data.
"Real" table:
let
Source = Table.FromRecords({[Date="16.01.2017", Sales = 1200], [Date="20.01.2017", Sales=1500], [Date="05.02.2017", Sales = 800], [Date="08.02.2017", Sales = 1100], [Date="13.02.2017", Sales = 300], [Date="10.01.2016", Sales = 1400], [Date="02.02.2016", Sales = 1800]}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}})
in
#"Changed Type"
"Plan" table:
let
Source = Table.FromRecords({[Date="31.01.2017", Sales = 3000], [Date="28.02.2017", Sales=2500], [Date="31.01.2016", Sales = 2800], [Date="29.02.2016", Sales = 2700]}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}})
in
#"Changed Type"
Your query for getting data, let's name it GetData:
let
t1 = Table.AddColumn(Real, "Source", each "Real"),
t2 = Table.AddColumn(Plan, "Source", each "Plan"),
MergedTable = Table.Combine({t1, t2}),
AddYear = Table.AddColumn(MergedTable, "Year", each Date.Year([Date])),
AddMonth = Table.AddColumn(AddYear, "Month", each Date.Month([Date])),
Group = Table.Group(AddMonth, {"Year", "Month", "Source"}, {{"Sales", each List.Sum([Sales]), type number}})
in
Group
You may avoid using last step by grouping results at report level, but this will result in users being able to view amount of each sale. If this is expected behavior, just remove Group step, or replace it with Table.RemoveColumn to remove [Date].
If you need separate columns for Plan and Real values, then pivot Source column.
Then just use GetData query in clustered column chart.
You will have to create a measure to calculate the Actual total and measure to calculate the Planned by each month or end of month. Then create a measure to compare the difference between Planned total and Actual total.
Actual =
CALCULATE (
SUM ( RealDate[Sales] ),
FILTER (
ALL ( RealDate ),
MONTH ( [Date] ) = MONTH ( MAX ( EndOfMonthTargets[Date] ) )
&& YEAR ( [Date] ) = YEAR ( MAX ( EndOfMonthTargets[Date] ) )
)
)
Planned = SUM(EndOfMonthTargets[Sales])
ActualVSPlanned = [Actual] - [Planned]
Now use that measures and the Date in eEndOfMonthTargets table to build a matrix:
For the ActualVSPlanned measure use conditional formating as follows:
You will get something like this: