How to write the query of it? - if-statement

Select all data from the games table and order the results by the cost of the production from the cheapest to the most expensive. If the cost of production is the same, order by ratings from best to worst.
column names (production_cost, rating)

SELECT g.*
FROM games g
ORDER BY production_cost ASC, rating DESC

Related

Merging queries on product number AND on date

For a report on bakery product performance I am trying to match the purchase price with the selling price in order to get to a product margin. The purchase price sits in a separate table and so does the selling price. Both tables contain product numbers so I am able to merge the two queries in order to get both selling price and purchase price on one line in order to compute a product margin.
The difficulty however is the fact that the selling prices and purchase prices are very subjective to change. Each line in both tables has a date field that is changing often. Therefore I would like to merge the queries in such a way that the selling price of a given product is corresponding to the purchase price of the same product, but also make sure that the date of the selling price and purchase price align.
How would I go about and do this?
Tables would look like this:
DATE - SHOP - PRODUCTNR - QUANTITY SOLD - TOTAL SALES - SELLING PRICE PER PRODUCT
DATE - PRODUCTNR - PURCHASE PRICE PER PRODUCT
You need to add a new column consisting of the [PRODUCTNR] and [DATE] fields. You can use a simple concatenate operation to achieve this:
For Example:
Let's say you have such a table:
Then you need to create a new column called [CompositeKey] in the table:
CompositeKey =
[DATE] & "-" & [PRODUCTNR]
Resulting Table:
Note: I used (-) character to separate columns. You can use any others, like pipe(|), underscore(_) etc...
Now you need to do the same for your 2nd table, and create a relationship between your [CompositeKey] columns on both tables. Now It will adapt to changing dates, and you will know which is which... I hope It solves your problem.

Is there a way to let the user choose the columns in a dax groupby formula in Power BI?

I have a table with spending information per week, gender and age group.
Sample Table = {
(1,"Man","0-10",9),
(1,"Woman","0-10",10),
(1,"Man","10-30",8),
(1,"Woman","10-30",2),
(1,"Man","30-60",4),
(1,"Woman","30-60",6),
(1,"Man","60+",9),
(1,"Woman","60+",8),
(2,"Man","0-10",6),
(2,"Woman","0-10",4),
(2,"Man","10-30",8),
(2,"Woman","10-30",8),
(2,"Man","30-60",7),
(2,"Woman","30-60",10),
(2,"Man","60+",3),
(2,"Woman","60+",8),
(3,"Man","0-10",3),
(3,"Woman","0-10",10),
(3,"Man","10-30",3),
(3,"Woman","10-30",10),
(3,"Man","30-60",1),
(3,"Woman","30-60",9),
(3,"Man","60+",8),
(3,"Woman","60+",5)
}
Using this table, I can calculate the amount of spending per group. A 'group' can be defined in different ways: 'Men' (only gender) or 'Men in age group <60' (gender + age group) or 'Men in age group <60 in week 25' (gender + age group + weeknumber) etc. To calculate the amount spent per group (in this case, groupby per gender+weeknumber), I make a new table use the following dax codes:
grouped spending =
VAR spending =
GROUPBY('Sample Table',
'Sample Table'[Weeknumber],
'Sample Table'[Gender],
"sum_spent_group",
SUMX(CURRENTGROUP(), 'Sample Table'[spent]))
RETURN
spending
And add a column to this table to calculate the part spent by the chosen group over the total amount spent:
spending part =
DIVIDE(CALCULATE(SUM('grouped spending'[sum_spent_group]),
ALLEXCEPT('grouped spending','grouped spending'[group])),
SUM('grouped spending'[sum_spent_group])
)
Now I can show the spending percentage of a group over the total amount of spending:
Is there a way I can let the user choose (using buttons/bookmarks/etc..?) choose which columns should be in the groupby? In other words; is it possible to make this code more general, and let the user specify the group combination? As well as for the numerator as denomerator.
I realize one solution is to make columns for all group combinations, but in my real data, I have many more categories, so that will take a lot of space (and a lot of work..).
Thanks a lot!
If I understand you correctly, you don't actually need to create a separate table for each group.
Create a measure to calculate sum of spent that will be influenced by slicers (your group). Something like spent_calc = SUM('Sample Table'[spent])
Create a measure to calculate total spent regardless of slicers selected CALCULATE(SUM('Sample Table'[spent]),ALL('Sample Table'))
Create measure that calculates percentage of total spent %_spent_by_selected_group = DIVIDE([spent_calc],[total_spent])
Create slicers and put percentage measure into a desired visual, e.g.
Now a user can create any combination of attributes and see the percentage of the total spent. The caveat is of course you will have to have a lot of slicers if you have a lot of categories, so it might not be the most elegant solution. Probably the easiest one though.

How do I use an IF THEN function in Power BI over multiple queries?

I am trying to filter an average amount (wage) from one query through a column (gender; 0 = male, 1 = female) belonging to different query.
So, I want put the different averages (query 1) for both genders (query 2) into one card or bar chart visualization.
Can I do this using DAX? Or do I need to merge the queries somehow?
Thank you!
Assuming you mean Tables vs Queries. If you have a relationship between the table with the Gender and a table with the averages, then you just need to include the gender column and the average column in your results.

How to switch data from two tables based on filter in Power Bi

I have two tables which have counts and sales based on dates and one of them also have customer ID. The counts are not same when we see by customer and summary. I also have customer filter on my dashboard. What I want to achieve is if no customer is selected the count should come from summary table otherwise it should come from customer if multiple or one is selected in the filter.
Customer Table
Summary Table
Any hints, I have tried lookupvalue function but I cant put date as search value from date table.
It's much easier to use Measures, instead of creating calculated tables to obtain those metrics. Also, summarized tables would not have the same filter context your are looking for.
Measure 1
Total Customers =
DISTINCTCOUNT('Customer Table'[CustomerID])
Measure 2
Total Sales =
SUM ( 'Customer Table'[Sales])

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")