I have a table like shown below:
ID
Date
Asset
Location
145
7/29/22
A
Market
145
7/30/22
A
Warehouse
145
7/29/22
B
Market
145
7/29/22
C
Truck
150
7/30/22
B
Market
145
7/29/22
D
Market
145
7/30/22
A
Market
What I am trying to accomplish is to get a distinct count of IDs for each date with a location filter as well. So I would want a count of ID based on the slicer selected Date of 7/29/22 AND 7/30/22 for the Market Location. The desired result is 2 for the selected dates from the date slicer which directly corresponds to the date column in the table.
I was trying to use this DAX formula and wasn't getting anywhere....
IDsMarket =
CALCULATE (
DISTINCTCOUNT ( 'Products'[ID] ),
ALL ( 'Products' )
)
I have a measure dropped onto a card. I should have specified that. My apologies. I need 1 measure to show me the combined count for the two days selected.
I tried this with countrows as well but of course the result wasn't distinct... Any help would be greatly appreciated!!
The formula you're looking for is
IDsMarket =
CALCULATE(
DISTINCTCOUNT('Products'[ID]),
'Products'[Location] = "Market"
)
The resulting Table will look like this
But if you put the measure on a Card visual, you'll get
So in DAX the same measure can yield 1000 different values - depending on the filter context.
I created a conditional column in Power Query and combined the ID with the "day" number from the date column which allowed me to then do a distinct count on that combined custom column which produced to correct answer. Sorry for all the confusion. One of those days.
Related
I'm using a simple table like this to make a report in Power BI:
Order number
Order date
Turnover
001
30/1/2022
10 €
002
30/1/2022
20 €
003
2/2/2022
15 €
I need to create a line chart showing all the dates, even where I have no data (no orders for that day). This is currently how is shown:
You can notice that the 1/2/22 and 3/2/22 are missing due to no order, but I want them to be visible and the value should be 0. This is also affecting the average line because it's calculated based on the days with data, but I need to put into account also the 0-turnover days.
I tried to use the "Show items with no data" on the date dimension and switch the X axis from Continuous to Catergorical and the other way around. I also tried to create a new metric like this:
Total Turnover = IF(ISBLANK(SUM(Orders[Turnover (EUR)])), 1, SUM(Orders[Turnover (EUR)]))
but it's not working.
If I understand your business requirement correctly, you are going to need to do three things:
Make sure you have a date-dimension table in your model. Build the relationship based on your [Order date] column.
Refactor your [Total Turnover] measure as such:
Total Turnover =
VAR TotalTurnover = SUM( Orders[Turnover] )
RETURN
IF(
ISBLANK( TotalTurnover ),
0,
TotalTurnover
)
Build your line chart using the [Date] column from your date table.
I have a date table called 'dDate', it looks like below:
Date
01/01/2020
02/01/2020
03/01/2020
I have another table called 'Calendar', which looks like below. Note that not all dates are present in this table.
Date Holiday
01/01/2020 1
03/01/2020 0
I want to add a column 'HolidayDate' to the dDate table, where, for a particular date, a value of 1 is given if 'Holiday'=1 from Calendar, else a value of 0. So the HolidayDate column looks for the date in the Calendar table, checks if holiday is 1 and returns 1 if it is.
So the output in dDate table should look like:
Date HolidayDate
01/01/2020 1
02/01/2020 0
03/01/2020 0
I want to add a new column and specify a formula which achieves the above. How can I do this?
If Calendar and dDate has relationship, you can create the following calculated column in dDate
HolidayfromCalendar = RELATED('Calendar'[Holiday])
If Calendar and dDate has no relationship, you can create the following calculated column in dDate
HolidayfromCalendar =
CALCULATE (
MAXX (
FILTER ( 'Calendar', 'Calendar'[Date] = MAX ( dDate[Date] ) ),
'Calendar'[Holiday]
)
)
If you create a simple relationship between you source table with the date and the table with the calendar, then you can use the holiday date for the source table and PowerBI will make the join for you. You don't need to "copy" the holiday indicator across. In fact, the holiday indicator belongs with the calendar table, since this is effectively your date dimension in a Kimball model.
As per comment, if you model is not yours to change, or you have painted yourself in to a bit of a corner with complexity, you can resort to using LOOKUPVALUE to pull the data across. Keep in mind this may have a performance impact on huge data sets.
I think that the following formula summarize pretty well what I want to achieve:
date diff =
ABS (
DATEDIFF (
data_table[login_date],
SELECTEDVALUE ( 'Date'[Date] ),
DAY
)
)
but it returns me the following error
A single value for column 'login_date' in table 'data_table' 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, max, count, or sum to get a single result.
In other word I want have a column in my data_table with date diff calculated dynamically based on min slicer date selection.
My final goal is to filter out dynamically users that has not been logged for the last 3 months based on the slicer date range.
Here is the dataset
user_id, login_date
111, 01/02/2021
222, 02/15/2021
444, 03/15/2021
555, 01/15/2021
I want user ID to be filtered out when the number of days between the max date of the date range and the day of the last connection is higher than 90 days.
Edit
I'm adding a different formula I'm working on but having few issues to make it work
active users = CALCULATE( DISTINCTCOUNT(data_table[id]), ( FILTER ( VALUES ( data_table[id] ), DATEDIFF(IF( ISBLANK(SELECTEDVALUE(data_table[login_date])),[Min range date],SELECTEDVALUE(data_table[login_date])),[Max range date],DAY) < 90 ) ))
You can't have a dynamically calculated column, but you can use a measure to do this. The issue that you have with your calculation is that it needed to do a row by row evaluation, rather than over a column. That is why you get an 'A single value for column 'login_date' in table 'data_table' cannot be determined' error.
In this case you can use SUMX, as this is a iterator and it will do row by row. So using the following measures:
Selected Date = SELECTEDVALUE('Calendar'[Date])
This reads the date selected. You can wrap it with a MIN/MAX if needed depending on how your slicer is set up. You can change the slicer to single select, it you just want one value.
Date Calc = SUMX('Table', DATEDIFF('Table'[Login_date], [Selected Date], DAY))
This uses SUMX to calculate on a row by row level.
You can then use this measure to drive your visual. In this example I've filtered out those over 30 days since the login
If you choose a new date, it will recalculate straight away. This should set you on the right path for your use case.
Power BI newbie here and I'm trying to figure how to craft my DAX to manipulate my measure values based on certain criteria in the other two tables.
Currently I have 2 separate tables which are joined by a One to Many relationship and a separate Measures table. (Total Sales Price is computed as sum of Sales Price)
My aim is to create a new measure where Total Sales Price is multiplied by 1.5x when DIM_Product_Type[Product Category] = "High".
New Measure =
CALCULATE (
SUM ( FACT_PriceDetails[Sales Price] ),
FILTER ( DIM_Product_Type, DIM_Product_Type[Product Category] = "High" )
) * 1.5
However this returns no values in my visual and I'm trying to discern if its a matter of the table joins or the DAX expressions.
Thank you for your time!
Your measure seems good.
It will select only those products with a Product Category of "High" and multiply them by 1.5 to give you result. i.e. Give me the sum of all "High" Product category Price details multiplied by 1.5.
What you need to check is:
Product Serial Numbers match across the two tables
Your Product Category does indeed contain the category "High"
You have entries in FACT_PriceDetails that link to a DIM_Product_Type that has a category of "High"
Check you have not set any filters that could be hijacking your results (e.g. excluding the "High" product category product type or the realated fact/s)
Option-1
You can do some Transformation in Power Query Editor to create a new column new sales price with applying conditions as stated below-
First, Merge you Dim and Fact table and bring the Product Category value to your Fact table as below-
You have Product Category value in each row after expanding the Table after merge. Now create a custom column as shown below-
Finally, you can go to your report and create your Total Sales measure using the new column new sales price
Option-2
You can also archive the same using DAX as stated below-
First, create a Custom Column as below-
sales amount new =
if(
RELATED(dim_product_type[product category]) = "High",
fact_pricedetails[sales price] * 1.5,
fact_pricedetails[sales price]
)
Now create your Total Sales Amount measure as below-
total_sales_amount = SUM(fact_pricedetails[sales amount new])
For both above case, you will get the same output.
I can't get a division correct with this sample data:
Calculated column Another calc. column
48 207
257 370
518 138
489 354
837 478
1,005 648
1,021 2,060
1,463 2,164
2,630 1,818
2,993 2,358
3,354 3,633
4,332 5,234
4,885 6,108
4,514 6,008
4,356 6,888
4,824 7,382
7,082 5,988
7,498 6,059
4,865
4,192
3,816
2,851
2,768
2,093
2,207
770
397
149
178
336
167
124
18
What I'm trying to do is to create a new calculated column.
For each row I want to get the value of Calculated column and divide it by the Total of Another calc. column.
The Total of Another calc. column = 82826
This is the desired output in a brand new calculated column, let's call it % Column:
% Column
0,000579528167484
0,003102890396735
0,006254074807428
.
.
.
NOTE - these 3 columns: Calculated column, Another calc. column and % Column are all in the same table and are all calculated columns.
I tried lots of formulas but not a single one returned the desired output. :| I guess it's because of the nature of calculated columns or I'm not getting the gist of it.
Is this even possible or I should follow another path using a Measure?
Can you shed some light?
####### EDIT #######
I put together a sample file to help debugging this. Here it is:
https://drive.google.com/open?id=1r7kiIkwgHnI5GUssJ6KlXBAoeDRISEuC
As you see:
Earned Daily % HARDCODED works just fine because 82826 is hardcoded as the denominator.
Earned Daily % by StelioK and Earned Daily % by Alexis Olson output the same wrong value for the division when using SUM formula.
I'm using the latest Power BI Desktop version if that matters: Version: 2.70.5494.701 64-bit (June 2019)
Basically, there is nothing wrong with the calculated columns, and both Alexis and StelioK formulas are correct.
The root problem here is a confusion between calculated columns and measures. You are looking at the results in a conceptually wrong way - through the matrix visual, with several filters active on slicers. If you remove the filters, you will see that the total amount is 140,920, not 82,826. The latter number is the total for the filtered data set, not the entire table.
To get this right, you need to understand several fundamental concepts behind Power BI:
Calculated columns are always static. Once a calculation is
completed, it can not respond to slicers or other UI controls. It's
just static data, identical to data in non-calculated columns. DAX
formulas used to calculate columns are active only when you create
them, or upon data reload.
If you want your calculations to respond to slicers etc, they must be measures. It's the only way, no exceptions.
Avoid calculated columns, they are utterly useless. Power BI is all about measures; I can't think of a single reason for using calculated columns. When you add a column, you are essentially enhancing your source data, because you feel like you are missing something you need for your report. But that need can be much better addressed at the source (database or file you import), or using Power Query, which is designed exactly for this kind of tasks. The best practice is: build your columns at the source, for everything else design measures.
Another important advice: never drop fields (columns) into visuals directly. Always write a DAX measure, and then use it. Relying on Power BI auto-aggregations is a very bad practice.
You can do this by using the following DAX:
% Column =
VAR TotalSum =
SUM ( 'Table'[Another Calc column] )
RETURN
IF (
NOT ( ISBLANK ( 'Table'[Calc Column] ) ),
CALCULATE ( DIVIDE ( SUM ( 'Table'[Calc Column] ), TotalSum ) ),
0
)
Which yields the following:
I Hope it helps!!
For me the following works:
DIVIDE( Table1[Calculated column], SUM(Table1[Another calc column]) )
If that's not working, I'd need to see a file where you can reproduce the problem.
Edit: After looking at your file, the total of 82,826 is only true with the filters you've selected.
Note that calculated columns are not dynamic and cannot be responsive to filters since they are calculated only when the table is first loaded.
If you need it to be dynamic, then write it as a measure more like this:
Earned Daily =
DIVIDE (
CALCULATE (
SUM ( 'Test data'[Value] ),
'Test data'[Act Rem] = "Actual Units",
'Test data'[Type] = "Current"
),
CALCULATE (
SUM ( 'Test data'[Value] ),
ALLSELECTED ( 'Test data' ),
'Test data'[Act Rem] = "Remaining Units",
'Test data'[Type] = "PMB"
)
)