Power BI - how to create measure based on conditions - powerbi

I have a base table 'Orders' with a column OrderNo, this column is being used in a visual.
I have another table 'Operations' with columns: OrderNo, OperationNo, TimeTaken (this is just a numeric column). This table is related to base table with OrderNo. Please note that one OrderNo can have multiple OperationNo.
I want to add a column to my visual 'TimeRemaining', which takes all OperationNo (from Operations table) for an OrderNo and sums the TimeTaken column.
How can I achieve this?

Power bi groups the data in a visual automatically.
if they are linked you can make a calculated column in Orders
TimeRemaining =
SUM ( OperationNo[TimeTaken] )
if you want a it in the Operations specifically then:
TimeRemaining =
CALCULATE (
SUM ( Operations[TimeTaken] ),
ALLEXCEPT ( Operations, Operations[OrderNo] )
)
NOTE.
the first is for a calculated column in Orders table
the second one is for the Operations table.

Related

Power BI - Comparing data at Row Level and concatenating the respective data in other column for same records

I want to show the consolidated data for similar record in separate column in power bi data.
I have a data with two columns and i want the result like below in third & fourth column
3rd column result nothing but comparing unique id in rows, e.g. 1=2 =false, 2=2=true
4th column result nothing but concatenation of Value column for unique record
could you please help to achieve this in power bi - i want to create custom columns for these two result in data table
We can use CalculatedColumn in DAX:
Result =
CALCULATE(
CONCATENATEX (
CALCULATETABLE ( VALUES('Unique'[Value] ) ),
'Unique'[Value],
", ",
'Unique'[Value],
ASC
), ALL('Unique'[Value])
)
And you Validation may be a measure:
Valid = if(SELECTEDVALUE('Unique'[Value]) = CALCULATE( max('Unique'[Value]), ALL('Unique'[Value])), FALSE(), TRUE())

Microsoft Power BI DAX - tricky situation using ALLEXCEPT

I have a situation in which I have a single excel file with one sheet, I am loading this single sheet (Sales) as a Power BI table.
I have two major dimension columns - Model and Product. I have a single numerical column called SalesAmount.
In all there are just 3 source columns in my Sales table.
One Model can have many Products, hence a 1:M relationship, but in the same (Sales) table.
I am using a Power BI matrix, as shown.
I have the following:
1)
Measure called [Sum of SalesAmount]: a simple sum of the SamesAmount column
2)
A Calculated Column called [ModelSalesAmount] in the Sales table:
ModelSalesAmount = CALCULATE(
SUM(Sales[SalesAmount]),
ALLEXCEPT(Sales,Sales[Model])
)
A Calculated Column called [ProductOnModel] in the Sales table:
ProductOnModel = Sales[SalesAmount]/Sales[ModelSalesAmount]
I have the matrix visual as shown.
The aggregated value for the ModelSalesAmount is incorrect.
HL Fountain Frame is a Model, and there are 8 products under this Model.
The Calculated column Sales[ModelSalesAmount] with a SUM aggregation in the matrix, works correctly at the product level (3,365,069.274), but not at the Model level. The Model level aggregation 26,920,554.19 is incorrect. I would want 3,365,069.274 at the Model level, NOT 26,920,554.19.
Interestingly, the Calculated Column Sales[ProductOnModel] with a SUM aggregation in the matrix, while using the Sales[ModelSalesAmount] Calculated Column in the denominator works correctly, both at the individual product level, as well as at the Model level!
What should I do to remove the undesired value 26,920,554.19, and make it 3,365,069.274 ?
I tried this below, but not working:
ModelSalesAmount_CC = IF (
HASONEVALUE(Sales[Product]) = TRUE,
CALCULATE(
SUM(Sales[SalesAmount]),
ALLEXCEPT(Sales,Sales[Model])
),
...........
)
Any suggestion please?
Change the Calculated Column [ModelSalesAmount] to a measure, I get the right output. Thanks Alexis.

COUNTIF in Power BI (New calculated column Required)

I want to calculate the running count of each value based on column A. In Excel, I am applying the following formula
=COUNTIF($A$2:$A2,A2)
I would like to get the same result in Power BI. Can you please advise.
With just a single column, this is impossible in DAX because duplicated rows cannot be distinguished as there is no inherent order to a column.
However, if you have an index column on the table (you can easily add one in the Query Editor), then it is possible to define such a calculated column so that it works similarly to the Excel formula.
CountIf =
VAR CurrentIndex = DATA[Index]
RETURN
CALCULATE (
COUNTROWS ( DATA ),
ALLEXCEPT ( DATA, DATA[ITEM] ),
DATA[Index] <= CurrentIndex
)

How to produce a snapshot table using Power BI measure

My intention is to populate days of the month to simulate a data warehouse periodic snapshot table using DAX measures. My goal is to show non-additive values for the quantity.
Consider the following transactions:
The granularity of my snapshot table is day. So it should show the following:
Take note that a day may have multiple entries but I am only interested in the latest entry for the day. If I am looking at the figures using a week period it should show the latest entry for the week. It all depends on the context fixter.
However after applying the measure I end up with:
There are three transactions. Two on day 2 and the other on day 4. Instead of calculating a running total I want to show the latest Qty for the days which have no transactions without running accumulating totals. So, day 4 should show 4 instead of summing up day 3 and day 4 which gives me 10. I've been experimenting with LASTNONBLANK without much success.
This is the measure I'm using:
Snapshot =
CALCULATE(
SUM('Inventory'[Quantity]),
FILTER(
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX( 'Date'[Date] )
)
)
There are two tables involved:
Table # 1: Inventory table containing the transactions. It includes the product id, the date/time the transaction was recorded and the quantity.
Table # 2: A date table 'Date' which has been marked as a date table in Power BI. There is a relationship between the Inventory and the Date table based on a date key. So, in the measure, 'Date'[Date] refers to the Date column in the Date table.
You can use the LASTNONBLANKVALUE function, that returns the last value of the expression specified as second parameter sorted by the column specified as first parameter.
Since LASTNONBLANKVALUE implicitly wraps the second parameter into a CALCULATE, a context transition happens and therefore the row context is transformed into the corresponding filter context. So we also need to use VALUES to apply the filter context to the T[Qty] column. The returned table is a single row column and DAX can automatically convert a single column, single row table to a scalar value.
Then, since we don't have a dimension table we have to get rid of cross-filtering, therefore we must use REMOVEFILTERS over the whole table.
the filter expression T[Day] < MaxDay is needed because LASTNONBLANKVALUE must be called in a filter context containing all the rows preceding and including the current one.
So, assuming that the table name is T with fields Day and Qty like in your sample data, this code should work
Edit: changed in order to support multiple rows with same day, assuming the desired result is the sum of the last day quantities
Measure =
VAR MaxDay =
MAX ( T[Day] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
T[Day],
SUM ( T[Qty] )
),
T[Day] <= MaxDay,
REMOVEFILTERS ( T )
) + 0
Edit: after reading the comments, this might work on your model (untested)
Measure =
VAR MaxDay =
MAX ( 'Date'[Date] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
Inventory[RecordedDate],
SUM ( Inventory[Quantity] )
),
'Date'[Date] <= MaxDay
) + 0

Get first department join date for each client from a different table using DAX

In my scenario, many clients moving between departments and the system is tracking this using a table with columns like: ClientID/ DepartmentID/ Start date.
What if I need to get a column with first department start-date for each client? I am trying to read this to a Client profile table from the above Department association table using DAX:
FIRSTDATE('ClientDepartment'[StartDate]) will give only a single date for all clients. I am looking to get something like:
For Example (see client, department movement and result needed tables:
This is for a tabular model and I am trying to add as a column (not as measure or calculated table).
I tried:
CALCULATE (
FIRSTDATE ( ClientDepartments[StartDate] ),
ClientDepartments[ClientID] = Clients[ClientID]
)
but throwing an error
The expression contains multiple columns, but only a single column can
be used in a True/False expression that is used as a table filter
expression.
If the two tables have an active relationship, such that the Clients table is a dimension table for the ClientDepartments table, then you can simply use the following expression for the calculated column:
CALCULATE ( MIN ( 'ClientDepartments'[StartDate] ) )
If you don't have a relationship in place, use a variable:
VAR currentClient = 'Clients'[ClientID]
RETURN
CALCULATE (
MIN ( 'ClientDepartments'[StartDate] ) ,
'ClientDepartments'[ClientID] = currentClient
)