Power BI-Compare 2 Text columns for equality using a measure - powerpivot

Data is similar to below:
OLD_ZEND comes from table1 and NEW_ZEND comes from table2. Looking to create a measure that will compare the strings of OLD_ZEND and NEW_ZEND and if they are the same output Y else output N.

I think what you need is a calculated column in one of your tables.
Create an index column in each table by right clicking table1 and select Edit Query, the Query Editor will be opened, in the Add Column tab select Index Column - From 1 then press Close & Apply. Do the same for the table2.
Then create a new calculated column in the table2, call it OLD_ZEND and use this DAX expression:
OLD_ZEND =
IF (
[NEW_ZEND] = LOOKUPVALUE ( table1[OLD_ZEND], table1[Index], [Index] ),
"Y",
"N"
)
Now you have a new column in table2 with Y or N depending if OLD_ZEND and NEW_ZEND are equal.
Let me know if this helps.

Related

Compare multiple text criteria to text field in single query?

I have a list of multiple text criteria that I need to use to count occurrences of matching text in the database with the LIKE operator.
How can I write a query that will run the comparison with all criteria at once and return a summarized list of counts per each of these criteria?
I've started with a simple query on which I'd like to iterate using a while loop. But unfortunately, I don't have the right to create a table that would contain the criteria to be referenced with each loop so I have to prepare query for each criteria separately.
SELECT sum(X), sum(Y), Z
FROM table
WHERE text LIKE 'criteria sample'
WHERE A = nnnnnn
AND B > 0
Group by Z
To avoid running and writing multiple queries, how can I load all criteria into one query to run them against the content to get the results faster out?
You can construct a table in the query with your values:
SELECT v.comparison, sum(t.X), sum(t.Y), t.Z
FROM table t JOIN
(VALUES ('criteria sample'),
('criteria sample 2'),
. . . -- whatever the values are
) v(comparison)
ON t.text LIKE v.comparison
WHERE t.A = nnnnnn AND t.B > 0
GROUP BY v.comparison, t.Z;
If you wanted every matching row, you could simply say WHERE text LIKE '%foo%' OR text LIKE '%bar%'. However, if I understand your question, you actually want a count of rows matching '%foo%' and a separate count of rows matching '%bar%'. This implies that the SUMs and GROUP BY have to be run separately for each criteria text.
One possible solution is to write all the queries separately, then union them at the end. Note that union requires every column to have a name. You'll also want a static identifier string in each subquery so you can tell which criteria was met. So you'll have something like:
SELECT sum(X) AS sumx, sum(Y) AS sumy, Z, 'foo' AS criteria FROM table WHERE text LIKE '%foo%' AND A = nnnnnn AND B > 0 Group by Z
UNION
SELECT sum(X) AS sumx, sum(Y) AS sumy, Z, 'bar' AS criteria FROM table WHERE text LIKE '%bar%' AND A = nnnnnn AND B > 0 Group by Z
UNION
...(etc)...
I think it should just work like this, if I'm understanding correctly.
select t.z,
sum(case when text like '%firstcriteria%' then 1 else 0 end) as countfirstcriteria,
sum(case when text like '%secondcriteria%' then 1 else 0 end) as countsecondcriteria
from table t
where A = 'nnnnnn'
AND B > 0
group by t.z
Like should follow with % symbol so that the word you are looking for is captured even if there a words before or after it. I also removed the redundant WHERE
SELECT sum(X), sum(Y), Z
FROM table
WHERE text LIKE '%criteria sample%'
AND A = 'nnnnnn'
AND B > 0
Group by Z

Sql column value as formula in select

Can I select a column based on another column's value being listed as a formula? So I have a table, something like:
column_name formula val
one NULL 1
two NULL 2
three one + two NULL
And I want to do
SELECT
column_name,
CASE WHEN formula IS NULL
val
ELSE
(Here's where I'm confused - How do I evaluate the formula?)
END as result
FROM
table
And end up with a result set like
column_name result
one 1
two 2
three 3
You keep saying column, and column name, but you're actually talking about rows, not columns.
The problem is that you (potentially) want different formulas for each row. For example, row 4 might be (two - one) = 1 or even (three + one) = 4, where you'd have to calculate row three before you could do row 4. This means that a simple select query that parses the formulas is going to be very hard to do, and it would have to be able to handle each type of formula, and even then if the formulas reference other formulas that only makes it harder.
If you have to be able to handle functions like (two + one) * five = 15 and two + one * five = 7, then you'd be basically re-implementing a full blown eval function. You might be better to return the SQL table to another language that has eval functions built in, or you could use something like SQL Eval.net if it has to be in SQL.
Either way, though, you've still got to change "two + one" to "2 + 1" before you can do the eval with it. Because these values are in other rows, you can't see those values in the row you're looking at. To get the value for "one" you have to do something like
Select val from table where column_name = 'one'
And even then if the val is null, that means it hasn't been calculated yet, and you have to come back and try again later.
If I had to do something like this, I would create a temporary table, and load the basic table into it. Then, I'd iterate over the rows with null values, trying to replace column names with the literal values. I'd run the eval over any formulas that had no symbols anymore, setting the val for those rows. If there were still rows with no val (ie they were waiting for another row to be done first), I'd go back and iterate again. At the end, you should have a val for every row, at which point it is a simple query to get your results.
Possible solution would be like this kind....but since you mentioned very few things so this works on your above condition, not sure for anything else.
GO
SELECT
t1.column_name,
CASE WHEN t1.formula IS NULL
t1.val
ELSE
(select sum(t2.val) from table as t2 where t2.formula is not null)
END as result
FROM
table as t1
GO
If this is not working feel free to discuss it further.

Postgresql how to update a column for max values for each row?

So I have a tablea that I need to upgrade the column highestprog with values from the table called buffer
If a point intersects with 2 buffers, one with 80 progression and another with 90 progression, the column should be updated with a 90.
So I thought the max operator should be used here. My query was as follows:
UPDATE test.tablea
SET highestprog = (SELECT max(b.progression) FROM test.tablea a, test.buffer b WHERE ST_Contains(b.geom, a.geom))
However, this just updates every single row in the entire table with a 100, instead of the correct value for each row. How to update with the correct value from the right buffer?
If I understand your question correctly, the maximum should be taken per point. Assuming that the table tablea contains an "id column" idx, one might proceed as:
WITH stat AS (
SELECT a.idx, MAX(b.progression) AS maxprog
FROM
test.tablea a, test.buffer b
WHERE ST_Contains(b.geom, a.geom)
GROUP BY a.idx
)
UPDATE test.tablea
SET highestprog = stat.maxprog
FROM stat
WHERE test.tablea.idx = stat.idx

How to use subquery result as the column name of another query

I want to use the result from subquery as the column name of another query since the data changes column all the time and the subquery will decide which column the current forcast data stored. My example:
select item,
item_type
...
forcast_0 * 0.9 as finalforcast
forcast_0 * 0.8 as newforcast
from sales_data.
but the forcast_0 column is the result (fore_column_name) of the subquery, the result may change to forcast_1 or forcast2
select
fore_column_name
from forecast_history
where ...
Also, the forcast column will be used multiple times in the first query. how could I implement this?
Use your sub query as an inline table. Something like....
select item,
item_type,
..
decode(fore_column_name, 'foo', 1, 2) * 0.9 as finalforcast,
decode(fore_column_name, 'foo', 1, 2) * 0.8 as newforcast
from sales_data,
(
select fore_column_name
from forecast_history
where ...
) inlineTable
I'm assuming here that the value from the sub-query will be the same for each row - so a quick cross-join will suffice. If the value will vary depending on the values in each row of the sales_data table, then some other type of join would be more appropriate.
Quick link to decode - in case you aren't familiar with it.

How to repeat a pattern in an MS Access query

I have a MS Access database and in it are two tables called [Pattern] and [Element].
The following example's show the tables and their respective datatype.
Table 1: [Pattern]
patternID - key
pStart - short date
pEnd - short date
Table 2: [Element]
elementID - key
patternID
text -text(2)
I want to create a query where it will repeat the pattern contained within the text field of the element table. For example
for patternID = 1 there are 4 elementID entries with the text values 1,2,3,4
How do I get a query to repeat 1,2,3,4,1,2,3,4 for as long as the difference between the two dates, pStart and pEnd in the pattern table?
Hopefully this makes sense, thanks in advance. I usually work in Excel so Access is new to me.
You will need a number or factor table with one field with integers from 0 to at least the maximum day count you will have.
Then you can create the first Cartesian query:
Select
patternID
From
Pattern,
Factors
Where
DateAdd("d", [Factor], [pStart]) <= pEnd
Save this as, say, Patterns and create a second Cartesian query:
Select
Element.patternID,
Element.elementID,
Element.Text
From
Patterns,
Element
Where
Patterns.patternID = Element.patternID
Order By
Element.patternID,
Element.elementID,
Element.Text
I'm unsure if you are doing this in the query designer or sql view. If your writing the sql, this might help:
select distinct E.text
from Element E, Pattern P
where E.patternID = P.patternID
AND p.Start <> p.End