Returning the first result of an table - powerbi

I'm struggling within DAX to find a formula to return the first result. My table is as folllows:
The unique column is Dim_B_ID. So what I really would like to have is to return the first result of Amount ONLY for Dim_B_ID where the column IN is not blank. Im struggling cause I get answers like 3500 (total of the rows) and I can't seem to integrate IF(NOT(ISBLANK( funtion into this. So if someone has a solution for me, I would really appreciate it as I'm not so much of an expert in DAX.

You could try using the following measure:
FirstNonBlank =
FIRSTNONBLANKVALUE(
'Table'[Dim_Date_ID],
SUM( 'Table'[IN] )
)
When put together with Dim_B_ID in a table for example you could visualize FirstNonBlank for every "categorie" like so:
Used slightly modified sample data here:

Related

How to use different calculation in one column using DAX?

I have a table like this:
I would like to calculate the result but some of ID column has different calculation formula.
For ID less or equal to 1733 using this formula: Value/3*100 for ID greater than 1733 using this formula Value*100
I tried this way:
Result =
IF('Data'[ID]<=1733,[Value]/23*100)
IF('Data'[ID]>1733,[Value]*100)
But it return an empty value. Anyone could help me please.
Thank you so much
This can be achieved using IF statement by following way by creating a calculated column called "Result"
Result = IF(Data[ID]<="1733",(Data[VALUE]/23)*100,Data[VALUE]*100)
Hope this helps !!
If the [ID] is a text then you can convert it to a number with the VALUE(). When you are trying to convert number to text with quotes then you will get a wrong result for some of values. For text comparison it works like this: "11111"<"2", for number 11111 > 2. For a calculated column it not necessary to write a full column name like 'Data'[ID], you can simply write [ID], because of a row context. DAX will understand that the value and row you are working with is in a current table.
Result =
SWITCH(
TRUE()
,VALUE([ID])<=1733,[VALUE]/23*100
,[VALUE]*100
)

Why using ALLSELECTED() AND FILTER() together?

I am trying to understand the below DAX code:
CALCULATE(
SUM(Revenue[Net])
,FILTER('Center', NOT 'Center'[Acc] IN {"RSM", BLANK() })
,ALLSELECTED()
,VALUES('Customer'[Customer Number])
)
I have the below questions:
What's the use of ALLSELECTED?? By definition ALLSELECTED returns all rows in a table, ignoring any filters that might have been applied inside the query, but keeping filters that come from outside. https://dax.guide/allselected/
So, what's the point of writing FILTER() if its going to be forced to be ignored by the next line (ALLSELECTED)?!?
Also by definition:
CALCULATE is just a expresion followed by filters...
What's the use of VALUES() ? It doesn't appear to be a filter, so how is it even allowed to appear there? (Per definition VALUES(): returns a one-column table that contains the distinct values from the specified column.)
I do not understand what is this returning? is it the SUM() or the VALUES()?
(I come from a SQL background, so any sql-friendly answer is appreciated).
In Dax every filter is a table of values its look similar to INNER JOIN;
ALLSELECTED is useful when you need to keep a row context (this is also a filter in DAX). You can use ALLSELECTED inside FILTER function.
For better understand what engine does you can use a DaxStudio with ServerTiming;
As you see this product one simple statement:
SELECT
SUM ( 'Table'[Cost Centre] )
FROM 'Table'
WHERE
'Table'[Project] NIN ( 'AB' ) ;
You can find useful article by Alberto Ferrari and Marco Russo:
https://www.sqlbi.com/tv/auto-exist-on-clusters-or-numbers-unplugged-22/
If it is only converting DAX queries if your connecting your sql analysis services to in DAX studio. because it is not working for PBI file data

Parsing DAX Query with Regular expression to get measures and dimensions

I want to create an application that uses Extended Events to analyze the usage of Azure Analysis Services models.
Specifically, I want to know what measures and dimensions the end users are using.
I look at the QueryEnd event and try to parse the TextData field. Depending on the tool used for querying the model I get either MDX or DAX in the TextData.
I think I have managed to parse the MDX with this RegEx: ([[\w ]+].[[\w ]+](?:.(?:Members|[Q\d]))?)
(from this post: Regular expression for extracting element from MDX Query)
Now parsing the DAX is the problem. If I query the model from fx PowerBI I get a DAX like this:
EVALUATE
TOPN(
502,
SUMMARIZECOLUMNS(
ROLLUPADDISSUBTOTAL('Product'[Color], \"IsGrandTotalRowTotal\"),
\"Order_Count\", 'SalesOrderDetail'[Order Count]
),
[IsGrandTotalRowTotal],
0,
[Order_Count],
0,
'Product'[Color],
1
)
ORDER BY
[IsGrandTotalRowTotal] DESC, [Order_Count] DESC, 'Product'[Color]
What I would like to match with the RegEx is:
'Product'[Color] and 'SalesOrderDetail'[Order Count]
And.... how would i know that Order Count is used as a measyre while Color is an attribute of the Product dimension?..... guess I won't?
Thanx a lot
Nicolaj
think I just found a possible solution for parsing both DAX and MDX queries:
([\[\'][\w ]+[\]\']\.?[\[\'][\w ]+[\]\'])(?!.*\1)
This will give me what I need.... without duplicates. Improvements are welcomed :-)
For disambiguating columns from measures, I would query the DMVs for the deployed model to get a list of columns and a list of measures. Then you can just look up your parsed tokens in the two lists:
DMV docs
Column DMV
Measure DMV.
Note that measure names are globally unique among the population of column and measure names, so that is an easy lookup (just throw away the table reference). Column names are only unique within the table they belong to.
It looks like you've already got a regex that's working for you, so run with that.
I'll also note that almost all PBI visuals which are returning anything other than a simple list (e.g. slicers or one column tables will return just a list) will follow the pattern of the query you shared.
Measures should all be in later args to SUMMARIZECOLUMNS. The pattern is:
SUMMARIZECOLUMNS (
<grouping columns, optionally in a ROLLUPADDISSUBTOTAL>,
<filters, defined above in VARs>,
<measures as ("Alias", [Measure]) pairs>
)

Return table with rows where certain column is blank

I thinks this is a very easy to answer question ,but I cant figure it out and im very beginner in DAX.
I have this sample data set:
I want to return a Table in powerbi with only the rows where job is blank. So it would look like this:
How to do this? :)
Because you mentioned DAX, you may use this expression to return a Table with only the rows, where job is blank:
FILTER('Table','Table'[job] = BLANK())
But I guess, it make sense for you only inside the measures, you want to calculate.
Click on the button in the title for job column and uncheck all values, except (Blank):

Automatic Data Transfer to DAX

I'm new in this so please pardon me for such a basic question.
I'm trying to pass some dates as filters to a measure.
I have a simple measure that counts opened items "last week".
The DAX I used:
OpenedLastWeek = CALCULATE(COUNTROWS(Table);Table[Created
Year]=2018;Table[Created Week]=45)
But there should be a way to pass values to the DAX code to replace manually entered "2018" and "45".
I tried Date Tables, extra tables with dates in it... I'm just confused.
Thanks.
In this case, I would probably use variables.
OpenedLastWeek =
VAR CreatedYear = --<Desired Year Calculation>--
VAR CreatedWeek = --<Desired Week Calculation>--
RETURN
CALCULATE(
COUNTROWS(Table);
Table[Created Year] = CreatedYear;
Table[Created Week] = CreatedWeek
)
Basically, you define the week and year how you'd like and then pass those variables into the CALCULATE function.
Thank you very much. It solved my problem.
For this case, I tried variables before but I was getting wrong results. I guess that was because I was using "CountA()" instead of "Countrows()".
Thanks again.
Have a nice day,
Eld