Is there a DAX formula to add a new column name for different criteria? - dax

I am struggling a little bit on Power BI - I want to be able to differentiate between two products based on the type of product and a create a new column with a new name based on the criteria, see example below.
EG
**Product Size Type
Mattress King Bespoke
Mattress Queen Bespoke
Mattress Single Regular
Mattress Double Regular
I want to be able to add the type on Power BI without having to do it manually so it automatically determines if the size of the mattress is King then it will add Bespoke in the column for type.
So far I am having to do it manually as this is how we get the raw data - it was fairly easy to do on Excel but since the migration to Power BI I have been struggling with the volumes to do manually.

You can use this calculated column:
Type Formula =
SWITCH(
'Table'[Size],
"King", "Bespoke",
"Queen", "Bespoke",
"Single", "Regular",
"Double", "Rgular"
)

Related

Power query - strategy for handling repeating rows

Given a report which as a table with repeated row headings, is there a good strategy for using Power Query/M to extract the data in a clean format?
For example the report available here, has an excel file (which at time of writing is pointing to August 2021):
https://www.opec.org/opec_web/static_files_project/media/downloads/publications/MOMR%20Appendix%20Tables%20(August%202021).xlsx
In this example:
we have the World demand table portion
Non-OPEC Liquids production portion
both of these have rows: Americas/Europe/Asia Pacific:
which makes it hard to distinguish them in Power Query
What is right approach which would allow extraction of data from this type of table?
I would add a column ... custom column ... with formula
=if [2018] = null then [Column] else null
and then right click the new column and fill down
That would put World Demand and non-OPEC as a column that you could additionally filter on

Is it possible to use a dynamic value in a DAX formula?

I'm working on a BI project with Visual Studio and Power BI.
I get the sales of several stores and in Visual Studio I made a column calculated with the following formula :
=
SWITCH (
TRUE(),
Y2_Sales' [storename] = "UK-RC O/O NEWQUAY", Y2_Sales [SalesExGST] / 0.8564058469475494,
'Y2_Sales' [SalesExGST]
)
To explain I get the sales in GBP and as soon as the store is Newquay I convert these values into euros to display them in my Power BI report.
The problem is that the value I put in hard is a value that moves every week, you can find it here :
https://www.xe.com/fr/currencycharts/?from=EUR&to=GBP&view=1D
Is it possible to modify this value dynamically by having historical exchange rates?
I don't quite understand why you want a calculated column, wouldn't a measure for euros be easier? Then you could declare your change rate as another measure and make the whole thing dynamic. You may need to use a disconnected table and selected measures to get the result you want, have a look at sumproduct for that https://www.sumproduct.com/blog/article/power-pivot-principles/ppp-variables-and-disconnected-tables-in-power-bi it looks like you're most of the way there, just lose the calculated column and go all measure!

DAX COUNT/COUNTA functions

I've looked at many threads regarding COUNT and COUNTA, but I can't seem to figure out how to use it correctly.
I am new to DAX and am learning my way around. I have attempted to look this up and have gotten a little ways to where I need to be but not exactly. I think I am confused about how to apply a filter.
Here's the situation:
Four separate queries used to generate the data in the report; but only need to use two for the DAX function (Products and Display).
I have three columns I need to filter by, as follows:
Customer (Display or Products query; can do either)
Brand (Products query)
Location (Display query)
I want to count the columns based on if the data is unique.
Here's an example:
Customer: Big Box Buy;
Item: Lego Big Blocks;
Brand: Lego;
Location: Toys;
BREAK
Customer: Big Box Buy;
Item: Lego Star Wars;
Brand: Lego;
Location: Toys;
BREAK
Customer: Big Box Buy;
Item: Surface Pro;
Brand: Microsoft;
Location: Electronics;
BREAK
Customer: Little Shop on the Corner;
Item: Red Bicycle;
Brand: Trek;
Location: Racks;
In this example, no matter the fact that the items are different, we want to look at just the customer, the brand, and the location. We see in the first two records, the customer is "Big Box Buy" and the brand is "Lego" and the location is "Toys". This appears twice, but I want to count it distinct as "1". The next "Big Box Buy" store has the brand "Microsoft" and the location is "Electronics". It appears once and only once, and thus the distinct count is "1" anyway. This means that there are two separate entries for "Big Box Buy", both with a count of 1. And lastly there is "Little Shop on the Corner" which appears just once and is counted just once.
The "skeleton" of the code I have is basically just to see if I can get a count to work at all, which I can. It's the FILTER that I think is the problem (not used in the below example) judging by other threads I've read.
TotalDisplays = CALCULATE(COUNTA(products[Brand]))
Obviously I can't just count the amount of times a brand appears as that would give me duplicates. I need it unique based on if the following conditions are met:
Customer must be the same
Brand must be the same
Location must be the same
If so, we distinctly count it as one.
I know I ranted a bit and may seem to have gone in circles, but I was trying to figure out how to explain it. Please let me know if I need to edit this post or post clarification.
Many thanks in advance as I go through my journey with DAX!
I believe I have the answer. I used a NATURALINNERJOIN in DAX to create a new, merged table since I needed to reference all values in the same query (couldn't figure out how to do it otherwise). I also created an "unique identity" calculated column that combined data from multiple rows, but was hidden behind the scenes (not actually displayed on the report) so I could then take a measure of the unique values that way.
TotalDisplays = COUNTROWS(DISTINCT('GD-DP-Merge'[DisplayCountCalcCol]))
My calculated column is as follows:
DisplayCountCalcCol = 'GD-DP-Merge'[CustID] & 'GD-DP-Merge'[Brand] & 'GD-DP-Merge'[Location] & 'GD-DP-Merge'[Order#]
So the measure TotalDisplays now reports back the distinct count of rows based on the unique value of the customer ID, the brand, and the location of the item. I also threw in an order number just in case.
Thanks!
I am semi new to DAX and was struggling with Count and CountA formula, you post has helped me with answers. I would like to add the solution which i got for my query: Wanted count for Right Time start Achieved hence if anyone is looking for this kind of answer use below, filter will be selecting the table and adding string which you want to
RTSA:=calculate(COUNTA([RTS]),VEO_Daily_Services[RTS]="RTSA")

How to update a calculated field based on the value of a field in the same record

I am developing an app for an RPG. I need to track ability scores and a corresponding modifier. I was going to put this in one table and it would look like this:
Table (Ability Scores)
Field 1 = Strength
Field 2 = StrengthModifier
If strength is an 8 or 9; then StrengthModifier would be 1.
If strength is a 10, 11, or 12; then StrengthModifier would be 2.
How would I do this in Access?
You could simply make [StrengthModifier] a calculated field whose Expression is
IIf([Strength]>=8 And [Strength]<=9,1,IIf([Strength]>=10 And [Strength]<=12,2,Null))
and whose Return Type is Long Integer.
Honestly, I think that you want this in at least four different tables. I won't assume to know what other data you're storing, just the data that's relevant to this question. Here's what I think they would look like.
ER Diagram
You would then write a query to relate the characters to their ability scores and those modifiers
Ability Query

Limiting AutoIncrement to a specific range

I am trying to create an application for work. The app will be used internally and should allow us to assign some barcode numbers to our product SKUs. I am using Visual Studio / Basic 2010 Express to build this as my very limited and beginners experience is with VS 2010 Express.
I'll give a bit of information about how I see this application working and then I'll get on with my actual question:
I see the app allowing us to create a new Product in the database by a user entering the SKU and description of the product and then the app will assign this product the next available base number for the barcode and from there the app will (if required) generate the correct EAN13 and GTIN14 barcodes and store them against that SKU.
As a company we have a large range of barcode numbers we can use and we have split this large range up so that the first 50,000 (for example) are for our EAN13 codes, the next 50K are for our GTIN14 codes for Inner Cartons and the remaining 50K are for Master Cartons.
So in order to achieve this I have my Product table which contains the fields 'SKU', 'Description' and 'BarcodeBase'. I have managed to set the BarcodeBase field as unique and I am attempting to use AutoIncrement(Seed & Step) to make sure that this assigns the product a base barcode (before I calculate the check digit) that falls within the EAN13 range as described above...
So finally my question is: Is there a way I can put an upper limit on AutoIncrement so that on the off chance, way way in the future, the base barcode number will not overflow into the next range?
I've been googling unsuccessfully for an answer and I am only coming across things which talk about the data type of the field having a limit. For example the upper limit of an Int32 type. Through my searches I have become vaguely aware of the 'Expression' property of the field and also the possibility of coding a partial class - but I don't know if that is the right direction to go in or if there is something much simpler that I am overlooking / have not found.
I would really appreciate any help!
Edit: As per GrandMasterFlush's comment - I have added a local database to my VS project. So I think I am using a SQL Server Compact 3.5 db.
Use a CHECK constraint, e.g.:
ALTER TABLE dbo.Product ADD CONSTRAINT ...
CHECK (BarcodeBase BETWEEN 1 AND 50000);
I suggest you do not make BarcodeBase an IDENTITY column in the Product table (IDENTITY is the feature that you are referring to as "autoincrement"). IDENTITY is really designed for surrogate key use only and isn't ideal for meaningful business data. You can't update an IDENTITY column, it isn't necessarily sequential, may have gaps in the number sequence and you also only get to use one IDENTITY column per table. Instead of using IDENTITY in the Product table you can generate the sequence elsewhere, for example by incrementing a single value stored in a single row table.

Resources