Is it possible to group by column1, but display column2 in DAX? - powerbi

I have a Fact Revenue, with 2 dimensions: Customer and Category.
I have the below data:
(In my pbix I have the below visual). There are just 1 or 2 customers that have changed category; so for that specific case, I see:
(This is correct, since the NewCategory has no revenue for the last year).
But, I don't want to see Revenue LYTD as blank; is there a way to display Category, but in fact do a group by Customer?... That way, I would be able to display:
Code is:
Revenue YTD:=CALCULATE ( [Revenue] , DATESYTD('Date'[Date],"31/05") )
Revenue LYTD:=CALCULATE ( [Revenue YTD] , SAMEPERIODLASTYEAR('Date'[Date]) )

I don't think something like that is possible, because logically it doesn't really make much sense.
In your desired outcome how do you know that it will show NewCategory if you don't group by it or specify it in any way? In some flavors of SQL that allow you to do that, it will show you the first category it finds (which in your specific case is OldCategory, but essentially it will be random and based on the order of rows in the table).
I would suggest first creating a mapping table, in which every old category corresponds to the relevant new category. Then join this table to your fact table. Finally, use a new category from the mapping table to group the revenue in the fact table.

Related

Drillthrough on Multiple Fields, then removing one of the filters in Power BI

I have run into an interesting use case that has been causing some headaches.
Basically, the end user wants to be able to click on the month/category on a stacked column chart, drill through to show all items in that category, but then also show the values for every month.
The data model basically has two tables. One contains all the transactional detail (Table A).
The second table contains a monthly snapshot (Table B), with both being linked by an item-month key.
The snapshot contains the categories for each item/month combination. There is also a category on the snapshot for table B, "Idle", which by definition would not have values for the specific month on Table A.
So basically to frame it another way, I am wanting a measure that will do something like this:
For all items in the selected month/category of Table B, return all values from Table A.
Because the resulting visual will be drillthrough, I think it also will basically guaranteed be filtered by the status/month.
Any help on this would be appreciated, would be happy to clarify at all.
This is the code I had come up with so far. It appears to work in cases where data exists, but not situations where the aforementioned Idle items appear. (ex: an Idle Item may have values for 11/12 months, but trying to drillthrough on the period where it does not have values results in slicers not working on the drillthrough page, which may be another issue altogether)
The "DistinctDates" table is a disconnected date table containing each distinct year/month from table B.
Measure =
VAR MaxDate = MAX(DistinctDates[DATE])
VAR MinDate = MIN(DistinctDates[DATE])
Var Output=
CALCULATE(
SUM(TABLE_A[VALUE])
, FILTER(
ALLEXCEPT(TABLE_B, TABLE_B[ITEM]),
TABLE_B[DATE] >= MinDate
&& TABLE_B[DATE] <= MaxDate
&& NOT ISBLANK(SELECTEDVALUE(TABLE_B[ITEM]))
)
)
Return Output

Power BI - Need a slicer to look at multiple columns of Data

I am learning PowerBI as i go along and have been able to solve most issues faced by a quick google. Unfortunately this problem has baffled me.
We have thousands of lines of data which include a "Home Country" column & "Away Country" column.
What we need our slicer to do is to is to pick up for example Australia in both of these columns and display the results.
I currently have a table created using the below:
slicercountrytable = distinct(
Union(
Values('All Data'[Home Country]),
Values('All Data'[Away Country])))'''
and then a measure:
Measure =
if(
Min('All Data'[Home Country]) in values (slicercountrytable[Country])
|| Min('All Data'[Away Country]) in values (slicercountrytable[Country]),
1,
Blank()
)
And have also tried the below measure:
Measure 3 = VAR
Searchvalue=search(SELECTEDVALUE(slicercountrytable[Country]),SELECTEDVALUE('All Data'[Combined Country]),,Blank())
Return
If(Searchvalue > 0,"Found")
I need the slicer to control the entire sheet however the above are doing nothing.
Thanks
In that case, instead of building some complicated DAX I suggest to work the model.
Let's say your current table looks like that:
id
home country
away country
1
A
B
2
B
C
You may want to deal with that in another table by unpivoting home_country and away_country in PowerQuery.
In order to have a country table like that:
id
country
attribute
1
A
home
1
B
away
2
B
home
2
C
away
Then you link this new table to your existing one on id and filter/slice in it.
I reproduced your example and feel that it is already showing the desired behavior. I think the problem you might be running into is that you will now need to add your 'measure' filter to each and every visual individually. It cannot be added to 'Page' or 'All Pages' filter areas. This is because of the way a measure's calculation varies across context and cannot be avoided.
DAX
Table 2 = distinct(union(all('Table'[Away]), all('Table'[Home])))
Measure =
if(
MAX('Table'[Away]) in VALUES('Table 2'[SelectedCountries])
|| MAX('Table'[Home]) in VALUES('Table 2'[SelectedCountries]) ,
1,
blank()
)

How to get the last score per entity before measure (date) in a new measure

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)

Filter by last not blank date in Power BI

I have data from multiple countries on a monthly basis. Since the updates are not regular, I want to set up filter to visuals, so they would show the last month for which I have data from all the countries. I have data from each country loaded into a separate dataset, which then are merged into one big. Is there an easy way to place such filter? I managed to use "LASTDATE" function in each of country sets to find which date is last, but if I try to filter with that measure, I simply get nothing in a result. Thanks!
Well, this feels a little clunky to me but I believe it will work for you. There are two steps. The first is to create a summary table that reads through your data and counts the number of distinct countries that you have in each month. This will be a new table in your model, so go into the modeling tab, click 'New Table' and add this DAX. Obviously, correct for your table and column names.
SUMMARIZED_ROWS = SUMMARIZE(
'Table1'
,Table1[Month]
,"CountOfCountries"
,DISTINCTCOUNT(Table1[Country])
)
Now add a measure to the table (or anywhere) like this:
MonthWithMostCountries = CALCULATE(
LASTNONBLANK(SUMMARIZED_ROWS[Month], 1 )
, FILTER(SUMMARIZED_ROWS, SUMMARIZED_ROWS[CountOfCountries] = MAX(SUMMARIZED_ROWS[CountOfCountries]) ) )
This is going to give you the month where you have the most distinct countries in your data. You'll want to look at it in a card or similarly isolated visual as it is a measure and can be affected by filter context.
So, on the left is my mock data - 3 countries, 3 months each with a 1 month stagger. On the right you see the result of the Summarize table. Then the measure showing the final result.
Hope it helps.

Power BI cumulative count with multiple condition

Did anyone know how to convert RunningCount into Power bi Dax ?
I test on RunningTotal, Rankx but seems not working.
The [year] is just a text column not in Datetime format.
I still new on this and not sure if my explain is good enough or not. Sorry for any inconvenient cause.
I try to create measure/ calculation column in power bi from the formula below.
I need the count by Year, break by column like product, customer, rate and category.
Before I get into my solution, there are a few assumptions I had to make:
The "Year" column is supposed to be years, and not five digit numbers. So in my data I dropped the second "2" in each (i.e. "20213" -> "2013").
You may have another column in your data to break ties, but given the data you provided, there is no way to rank the first and third lines (they both have product ABC and year 2003).
Given those assumptions, here is my solution...
First, here is what my data looks like. I added an ID column so that we can see every row, even duplicates.
From there, you can simply add a new column with the following formula.
Running Count =
COUNTROWS(
FILTER(
'Data',
[ProductName] = EARLIER([ProductName]) &&
[Customer] = EARLIER([Customer]) &&
[Seller] = EARLIER([Seller]) &&
[Year] <= EARLIER([Year])
)
)
The EARLIER function is being used to specify a ProductName, Customer, etc. from the row of the table to be used in the filtering of the data. Once we have the data filtered, we can simple count the number of rows.
The final result looks like this. As noted in my second assumption, there is no way to break ties, so my numbers are slightly off from what you have in your screenshot