Create a custom table inside a dax measure and count number of values of an specific column - powerbi

I'm practicing my Power BI skills. I've downloaded a csv file which contains data about olympic games. The dataset has many columns, such as country, athlete name, year, sport, event, medal which the athlete has won, olympic city, etc.
The problem is that I want to create a bar graph that display country name by medal types count. However if create a graph "Country" by "Medal" from original csv it will not display the correct numbers of medals, because if a country wins a medal in a team sport (like volleyball or football) it should count as only one medal, and not the sum of all medals of all athletes in that team. This could be solved by removing Athlete column and selecting distinct values of "Event" column, like creating a table using the following formula:
Table 2 = CALCULATETABLE(ALLEXCEPT('summer (3)','summer (3)'[Athlete]),DISTINCT('summer (3)'[Event]))
However, I don't want to create a new table, because I would have serious problems with relationship between them (I have no idea how to do it, to be honest). So I want to create a measure. I created the following measure:
Medal count = COUNTX(CALCULATETABLE(ALLEXCEPT('summer (3)','summer (3)'[Athlete]),DISTINCT('summer (3)'[Event])),'summer (3)'[Medal])
It is showing the correct number of all medals in olympic games history (untill 2012). However, for every country, its showing the number of gold medal, silver medal and bronze medal with the same number (the total number of olympic medals 14753). It's not filtering by the number of rows for that specific country.
The same number also appears if I select any medal type from filter option (Gold, Silver or Bronze).
I have no idea how to fix this. How can I create a measure that shows the correct number of medal type for every country?

This is what I would do. First I will create an "id" column if I haven't had that, then I will do the distinctcount on that.
The DAX for the id column should be something like this:
debug_id = CONCATENATE(Table['Year'],CONCATENATE(Table['Sport'],CONCATENATE(Table['Discipline'],CONCATENATE(Table['Country'],CONCATENATE(Table['Event'],Table['Medal'])))))
then you can basically drag and drop this field onto the x-axis (y-axis and legends stay the same) and select Count (Distinct). If you really want the measure for this, it should be quite straight forward like:
Medals count = DISTINCTCOUNT(Table['debug_id'])

Related

Power BI - Create table from Measure

In Power BI I have a list of inventory transactions (InventTrans) with date, quantity and value for movements in and out of the inventory.
There are 1 million lines of inventory transactions, and the sum of all lines for each ItemID gives the inventory value of that item.
I have created a measure to calculate number of days from last movement until today:
Days since last movement = DATEDIFF(CALCULATE(MAX(InventTrans[Date]),TODAY(),DAY)
and a measure (InventoryStatus) to group each ItemID based on this result.
I have used
InventoryStatus = IF([Days since last movement]<183, "Movement last six months", ("Six months to a year", "Over 1 year". and so on)
Now I want to create a table with ItemID and the measure InventoryStatus.
My aim is to use InventoryStatus as a filter, so maybe there is better way to achieve this without creating a new table with ItemID and InventoryStatus.
Assuming inventory status is a property of the item, not the transaction, your proposed approach is probably correct. The data model will be more intuitive and more efficient if you create an items table that has ItemID, InventoryStatus, and any other item-level data. After linking the two tables together InventoryStatus can be used as a filter for transactions.

PowerBI calculated column - translate question

I want to create a calculated column that can translate several values into something that is more readable.
My example is something like a list of company departments, most are formatted with a formal name in the example below I'd like to calculate a column to convert acctng to Accounting
Sales
Marketing
Manufacturing
Support
acctng
So the calculated column would read
Sales
Marketing
Manufacturing
Support
Accounting
I'd like to create a calculated column that transfers over all items as they are except, when we see accting it's converted to Accounting. I know that I can do a transformation in the data view, however, I'm hoping for a dax formula.
Thanks!
If you want to do that with DAX, then you can create a new column and use SWITCH function:
Department (Translated) =
SWITCH(Sales[Department];
"acctng"; "Accounting";
"smtng"; "Something else";
Sales[Department])

Create Custom Column with IF and lookup functionality to own table

I have merged two tabels ( sales and forecast ). For all the rows coming from sales query the cost price column has a value. The forecast rows does not have that.
In order to calculate future metrics/KPI I need to make a Power Query transformation that populates cost price on all forecast rows. I would like to do some kind of refence to the ProductName (exits both on the sales and forecast rows) and pull the cost price from the sales rows. The ProductName can have multiple entries in the table, but will be the same for alle the rows. So maybe a find first/max or something would be fine.
However, I am not sure have to make this calcuated column with some sort of lookup to ProductName?
Well you can definitely do so
Here is an excellent Article form Microsoft on LookupValue
In addition check this Thread as well. It will give you more Idea.
I would do something like
=LOOKUPVALUE(Product[SafetyStockLevel], [ProductName], " Mountain-400-W Silver, 46")

DAX Calculate Sum of sales per productid filter by productid ( NOT IN TOP 20 )

I am fairly new to PowerBI DAX and I want to filter out the top 20 product ids in a measure.
I came up with this formula but it does not seem to be working and I was hoping to get some help here.
$ Amount Parcel =
CALCULATE(
SUM(Data[$ Amount Parcel]),
FILTER (Data, NOT (Data[idProduct], SUM(Data[NetSales])) IN TOPN(20, SUMMARIZE(Data, Data[idProduct], "NetSales", SUM(Data[NetSales]))))
)
I want to show sales per PID for all products except for our 20 best sellers.
Thank you !!
I would suggest an easier approach adding a dimension column.
First of all, you need to have Product dimension table separated from Sales fact table. Make sure to create one-to-many relationship between Product and Sales with "Single" cross filter direction.
Then you can create a calculated column on Product table, which can be used to filter out top selling products.
Sales Rank = RANKX('Product', CALCULATE(SUM(Sales[SalesAmount])))
Now drag and drop Sales Rank field into the Filters pane of your visualization, and set the filter condition so that top selling products will not be shown.

Dax Filter the count of If

I am trying to create a calculated column for Champions. Each year, a team that attains 4 wins in the final round is the champion. Basically, I need a DAX statement that translates to "when a Team in a given year has a count of 4 wins in the final round they are that year's champion"
I just don't know how to do this in DAX. I have tried a summary table to no avail. I know I need to Calculate and Filter but it is the Count of Wins for a given year that is given me trouble. Essentially, the Image below should have a champions column. Ignore the title of this image, it is supposed to say "Count of Finalists"
You may show only winner team in table by applying visaul filter using this measure,
Is Champion = IF ([Count of champion] = 4 ,"YES","NO")