I have a report in Microsoft VS 2010. It contains various fields. One of the fields is Order Type. One of the other fields is Quantity Sold. I want to be able to make the Quantity Sold value a negative number if the Order Type is a certain value. What is the best way to do that?
This will negate the value under some condition:
select OrderType,
case when OrderType = 42 then -QuantitySold else QuantitySold end as QuantitySold
from StuffThatWasSold
Related
I have a Dashboard with different visuals.
Data is made up of different values for insurance companies.
I want my slicers/filters to not filter all data, but to only highlight the chosen company.
For example, in my slicer I choose the insurance ABN.
Instead of showing me the value for ABN only in my visuals, I want all other values to still be visible and ABN's value to be highlighted in the visuals.
Does anyone know how to do this?
You can use conditional formatting to achieve this. Lets say that we will change the background color to "highlight" a row (or cells, to be precise).
First, we need a slicer, which will not filter our data. We can do this by duplicating our source table, removing the unnecessary columns and making sure there is no relationship between the source and the duplicate. So if we have a source table, named Table, like this:
Right click on it and select Duplicate:
Then right click the title of the column you want to keep and select Remove Other Columns to get a list of company names only (you may also remove the duplicates, but it's not required). Then in the model delete the relation between both tables:
Now you can place a table showing company name and sales from your data source, and a slicer for company name from the duplicate table. At this point selecting values in the slicer should not affect the table.
Now you need to capture the value of the slicer and use it in a measure, which will determine should current row be highlighted or not. You can use SELECTEDVALUE for that, but note that it will give you a value only if there is a one selected in the slicer. If you want to support highlighting of more than one company, it gets a bit more complicated.
Make a new measure in your source table, like this:
Measure = IF(HASONEVALUE('Table (2)'[Company name]);
IF(SELECTEDVALUE('Table (2)'[Company name]) = MAX('Table'[Company name]); 1; 0);
IF(ISFILTERED('Table (2)'[Company name]) && COUNTROWS(FILTER('Table (2)'; 'Table (2)'[Company name] = MAX('Table'[Company name]))); 1; 0))
In case there is only one value selected in the slicer (see HASONEVALUE), then our measure will return 1 (highlight) or 0 (don't), comparing it with the current row.
Otherwise (i.e. there is no selection in the slicer, or there are 2 or more companies selected), then we will look at the filtered list of companies (Table (2)) - if it contains current row, then 1 (highlight), otherwise 0 (don't). But we will also handle the case, where there is no value selected in the slicer. In this case the list will contain all the companies, i.e. all rows will be highlighted. Here comes ISFILTERED. And at the end, if the list is filtered and current row exists in the filtered list, then 1 (highlight), otherwise 0 (don't).
Now, you need to use this measure to change the background of the column - right click each column in your table and select Conditional formatting -> Background color:
Then format by rules, where Measure >= 1 like this:
Now, when there is no selection in the slicer, there are no rows highlighted in the table:
If you select one company, it is highlighted:
It also work if there are multiple companies selected:
Thank you Andrey for your step-by-step explanation which as been incredible helpful. I'd like to follow up with a further question, particularly regarding the comment below.
"You can use SELECTEDVALUE for that, but note that it will give you a
value only if there is a one selected in the slicer. If you want to
support highlighting of more than one company, it gets a bit more
complicated."
In my model, I've linked a third table (Table (3)) to Table (2) with a many to one relationship with Table (2). Therefore when I click on Table (3), it will filter Table (2), which acts as a slicer for Table (1).
When only 1 value is filtered in Table (2), it conditionally formats the cells in Table (1). However, when more than 1 value is filtered in Table (2), conditional formatting fails.
As I'm looking to avoid manually selecting multiple values in the slicer (Table (2)), I was wondering if there's a workaround for SELECTEDVALUE such that it is able to conditionally format when I filter more than 1 value in Table (2).
I have a column called internal_code in my Customer model. Since some users may use it as alphanumeric and others as only numeric, I need to suggest a number to the user before confirmation, in the UI.
I'm using this code right now:
previous_number = Company.where(:company_id => self.company_id).maximum(:internal_code)
But this is not working as expected, given for some reason, sometimes it's returning "999" when the latest value is "1000", or in another example "2290" when the latest value "2291".
I've been digging in the official documentation on maximum and calculate for Active Record, but didn't found if it's not intended to work with String columns. Maybe it's just obvious, but I wanted to ask here before I confirm my thoughts.
If this is a text column you may be getting the ASCIIabetical "max" instead of the numerical max. "999" sorts after "2291".
You need a numerical column type (e.g. INT) in order to do numerical maximums.
This should be a simple migration to change the column type if those values are purely numerical and fit in a 32-bit or 64-bit integer.
The definitive solution I found:
# Note: the ORDER clause is the KEY. Length means biggest number, ::bytea means sort it alphanumerically
filtered_result = Company.where.not(:internal_code => [nil,'']).where(:company_id => self.company_id).order("length(internal_code) DESC, internal_code::bytea DESC").first
previous_number = filtered_result.internal_code.scan(/\d{2,5}/).last
This complexity is given because of the possible values the users can put in this field. E.g.
"ABD123"
AS2134FG
AS34SD2342
Hope it helps in similar issues. Thanks all.
I have this Table.
I want to create a measure that will only work with the Products containing "HDD" in their name (first, second and fifth product in that case).
It's working with that formula : HDDProduct= CALCULATE(COUNT([Product]),FILTER(ALL([Product]),SEARCH("HDD",[Product],1,0)))
It only count the number of product with HDD in their name. But if I want a sum of the prices of those products for example, it won't work. I can only use COUNT.
Any idea ?
for sum you have to create another measure, below are my resolution to your question
for count
HDDProduct_count = CALCULATE(COUNT([Product]),FILTER(Product_table,SEARCH("HDD",[Product],1,0)))
for price sum
HDDProduct_Price_sum = CALCULATE(SUM(Product_table[Price]),FILTER(Product_table,SEARCH("HDD",[Product],1,0)))
note: please check the data type of price column to integer data type
download pbix file
I'm trying to create a workflow that would make the target field value a negative number. I want to create related records as credits and debits and then be able to sum them up to get a net value.
I've tried to update the field to -1 and then multiply it accordingly, but I get an error stating that the value needs to be between 0 and 1,000,000,000,000. I've also just tried to multiply the value by a -1, but that doesn't work either. It just runs the workflow, but doesn't change the value.
Building on the comment from #MarioZG it looks like your CRM field doesn't allow negative numbers.
When you setup a number field (Decimal; Currency; Floating or Whole Number) you can specify the range of acceptable values. Here's a quick screenshot of the Whole Number's properties:
I actually figured this one out. I created two different workflows to create records in one entity with different "Types". Then, on the account I had a rollup field to sum one type and then a rollup type to sum the other type. Then I used a calculated field to subtract one from the other.
Use data type of the field as decimal number(looking at your use), in this case you will have flexibility of storing all sort of numbers.
I want to match two strings entered in a form. The combination of the strings should be checked against existing values in a table to return a price. I have two battery types - MF and LM. They both have similar battery codes under their respective types i.e. N40 and N70. (So there is MF N40 & LM N40. There is also MF N70 & LM N70.) All have different prices. So based on the combination entered, the text box "Price" should be populated. How do I get "price" - from the combination of Battery type and battery code entered?
You can drop an expression right in the price textbox just like you would in Excel. DLOOKUP is the one you want. Once you get DLOOKUP working, nest it within IIF to avoid an error before a value is added to the other fields.