A circular dependency in Power Bi - powerbi

I calculated a field that brings the total sales for bikes in 2018-all work fine
copy the same formula to another column to calculate the same for 2017 brought this reference error -how can I move around it if I want to show a few columns using the same formula with different parameters
Sales_Bike_2018 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2018
)
Sales_Bike_2017 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2017
)

The problem is that you are creating 2 columns on a table that doesn't have a Unique Key.
When you create Sales_Bike_2018 everything works fine because it depends on the rest of the columns of your table but when you create the column Sales_Bike_2017, the code of Sales_Bike_2017 depends on rest of the columns as well as on Sales_Bike_2018.
Similary if it was possible to create Sales_Bike_2017 then Sales_Bike_2018 would have depended upon Sales_Bike_2017 and that's not allowed and that's why you get a circular dependency error.
Solution:
Remove filters coming from both new columns due to context transition by using REMOVEFILTERS ()
Sales_Bike_2018 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2018,
REMOVEFILTERS ( TrainingSample2[Sales_Bike_2017] )
)
Sales_Bike_2017 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2017,
REMOVEFILTERS ( TrainingSample2[Sales_Bike_2018] )
)

Related

Calculate the total from 2 tables - Powerbi

So, I have two tables I need to combine without merging as I was told merging the tables does not auto update when posted so had to separate my tables. So, I have Fioptics and legacy tables where I pull RecordID, JobTypeID and CustTypeID individually below is how my code looks
legacy res install =
CALCULATE (
COUNT ( LEGACY[RecordID] ),
FILTER (
LEGACY,
LEGACY[JobTypeID] = 1
&& LEGACY[CustTypeID] = 1
&& LEGACY[prod_grouping] = "legacy"
)
)
and
fioptics res install =
CALCULATE (
COUNT ( FIOPTIC[RecordID] ),
FILTER (
FIOPTIC,
FIOPTIC[JobTypeID] = 1
&& FIOPTIC[CustTypeID] = 1
&& FIOPTIC[prod_grouping] = "fioptics"
)
)
how do I go about using a dax function to pull from both RecordID's, JobTypeID and CustTypeID and filter from my FIOPTICS AND LEGACY tables at the same time? It might be a simple answer but having a brain fart maybe overlooking a simple solution to this problem.
You can combine those columns in a calculated table
Combined =
UNION(
SELECTCOLUMNS(
LEGACY,
"RecordID", LEGACY[RecordID],
"JobTypeID", LEGACY[JobTypeID],
"CustTypeID", LEGACY[CustTypeID]
),
SELECTCOLUMNS(
FIOPTICS,
"RecordID", FIOPTICS[RecordID],
"JobTypeID", FIOPTICS[JobTypeID],
"CustTypeID", FIOPTICS[CustTypeID]
)
)
And of cause you can filter the combined table in your report to your needs.

PowerBI report cannot recognize If statement when string are compared

I'm trying to replicate the example presented in this youtube tutorial
https://www.youtube.com/watch?v=z9ttZAZkEhs
However, even if I use the same DAX code the controls do not recognize the values properly.
Selected = calculate (DimProduct[uniqueCustomer], treatas( Values(Products[Name]), DimProduct[EnglishProductName] ) )
I tried different ways to recognize the values coming from the slicer, but they simply do not work.
CheckColumn = if (trim(DimProduct[EnglishProductName]) = trim(DimProduct[SelectedNumber2]),true,false)
I have attached the example file that I'm using.
https://github.com/gabrielacosta/TestPowerBiSlicer/blob/main/testslicer.pbix
Does anyone know what could be the issue.
I guess your calculation should look like the formula below but it doesn't make much sense doing that calculation.
Selected =
CALCULATE (
[uniqueCustomer],
TREATAS ( VALUES ( Products[Name] ), DimProduct[EnglishProductName] ),
VALUES ( DimProduct[EnglishProductName] )
)
For this specific scenario just using SELECTEDVALUE is better
Selected2 =
VAR SelectedProduct =
SELECTEDVALUE ( Products[Name] )
VAR CountCalc =
CALCULATE (
[uniqueCustomer],
FILTER ( DimProduct, [EnglishProductName] = SelectedProduct )
)
RETURN
CountCalc

Calculated Column: Distinct id# should return first date and ignore other dates

I have the requirement in which id# has duplicate records and also has duplicate received_date, where I need to show only unique received date for each id#. Could you please help me on how to resolve this?
Data sample shown below:
I have tried the following in the calculated column
expected_date_or_result =
VAR selected_id = test[id#]
VAR distinct_received_date =
CALCULATE (
FIRSTDATE ( test[received_date] ),
FILTER ( test, test[id#] = selected_id )
)
RETURN
distinct_received_date
I am not sure now to add blanks in case of duplicate received_date.
Please help me with this.
Note: I cannot use remove duplicate option since it is affecting my column group
There are likely many ways to approach this but here's the first one that comes to my mind:
expected_date_or_result =
VAR TopRow =
TOPN (
1,
FILTER ( test, test[id#] = EARLIER ( test[id#] ) ),
test[received_date], ASC,
test[group], ASC
)
RETURN
MAXX (
FILTER ( TopRow, test[group] = EARLIER ( test[group] ) ),
test[received_date]
)
This picks the top row of the table filtered by id# and sorted by received_date and group and then filters that row so that it's only non-empty if the group is the top one and extracts the received_date column using MAXX.

PowerBI: Calculate column containing current firmware based on occational messages

I have a table called MachineEvents containing data similar to the one seen above.
I'm trying to create a measured column in DAX containing the current firmware of the machine.
This will be used to filter only messages when the machines has had certain firmware.
I've tried searching around but i have trouble finding similar problems.
Assuming you want your Firmware column to return the latest value from field message_info where message_type = 1, based on event_time, then use this DAX code in your Calculated Column:
Firmware =
VAR LastFirmwareTime =
CALCULATE (
MAX ( MachineEvents[event_time] ),
FILTER (
ALLEXCEPT ( MachineEvents, MachineEvents[machine_id] ),
MachineEvents[message_type] = 1 && MachineEvents[event_time] <= EARLIER ( MachineEvents[event_time] )
)
)
RETURN
CALCULATE (
VALUES ( MachineEvents[message_info] ),
FILTER (
ALLEXCEPT ( MachineEvents, MachineEvents[machine_id] ),
MachineEvents[message_type] = 1 && MachineEvents[event_time] = LastFirmwareTime
)
)
See worked example PBIX file here: https://pwrbi.com/so_60372050/

DAX - Stuck on dynamic MAXX column formula

I'm working in Power BI.
I have a table with member card usage data called NonSameDayUses:
https://www.screencast.com/t/yeSjoqonZ
I have another table with member card add data called AddsOnly:
https://www.screencast.com/t/zlPBRWaDqC
The tables are related by the GUID_TranDate2 field. I am trying to add a column to NonSameDayUses that provides the date just before the use date (to calculate when the amount used was added to their card). I have tried a million things, but this is my current formula and I can't figure out what is wrong with it:
DateAdded =
MAXX (
FILTER (
AddsOnly,
AND (
AddsOnly[member_guid] = [member_guid],
AddsOnly[ValueAddDate] < [TransactionDate]
)
),
AddsOnly[TransactionDate]
)
Neither filter is working for me. If I try it with just the first argument (member_guid), I get blanks. If I try with the second (dates) I get the max date for the whole table with no filtering.
Any help would be sooooooooooo appreciated, as I am currently banging my head against the wall! :)
Try qualifying all the column names, it should work:
DateAdded =
MAXX(
FILTER(
AddsOnly
, AND(
AddsOnly[member_guid]
= NonSameDayUses[member_guid]
, AddsOnly[ValueAddDate]
< NonSameDayUses[TransactionDate]
)
)
, AddsOnly[TransactionDate]
)