I have a string which has at least one digit per bracket. Now, I want to extract the digit(s). How do I do this in Redshift sql?
ColumnA ColumnB (output)
(,,,3,) 3
(2,,,) 2
(,,,1) 1
(1,,,3) 13
You could use REGEXP_REPLACE. Here's a snippet:
CREATE TABLE x (col1 varchar(255))
INSERT INTO x VALUES ('(,,,3,)'),('(2,,,)'),('(,,,1)'),('(1,,,3)');
select col1,
regexp_replace(col1,'[^\d]','','g') as col2
from x;
col1
col2
(,,,3,)
3
(2,,,)
2
(,,,1)
1
(1,,,3)
13
Try it in SQLFiddle
Jakob's answer would work. You can also do the same thing with REPLACE:
CREATE TABLE x (col1 varchar(255))
INSERT INTO x VALUES ('(,,,3,)'),('(2,,,)'),('(,,,1)'),('(1,,,3)')
SELECT REPLACE(
REPLACE(
REPLACE(
col1, ',', ''
) ,')', ''
), '(', ''
) FROM x
replace
3
2
1
13
SQLFiddle
I am new to DAX, I have a data which looks like -
col1
col2
A
20
A
10
B
30
B
20
My output should be -
col1
col2
col3
A
20
30
A
10
30
B
30
50
B
20
50
I have tried writing a measure but it dosnt work -
col3 =
CALCULATE (
SUM ( Sheet1[col2] ),
FILTER (
ALLSELECTED ( Sheet1 ),
Sheet1[col1] == Sheet1[col1]
)
)
If you are actually looking for a formula to calculate col3, take this one here and put all cols into a table visual.
col3 =
CALCULATE(
SUM('Sheet1'[col2]),
ALLEXCEPT(
'Sheet1',
'Sheet1'[col1]
)
)
Let's say I have a table like this:
column1 Column2
A 01/01/2020
B 01/01/2020
C 04/01/2020
A 07/01/2020
B 07/01/2020
A 12/01/2020
C 10/01/2020
What I am trying to do is count how many times a value in column1 has occurred. So I want to be able to end up with this:
column1 Column2 column3
A 01/01/2020 1
B 01/01/2020 1
C 04/01/2020 1
A 07/01/2020 2
B 07/01/2020 2
A 12/01/2020 3
C 10/01/2020 2
I found DAX to count how many times in total a value occurs but I can't seem to find how to count them cumulatively. Thanks in advance.
The logic is the same as with a cumulative sum, just with COUNT instead:
column3 =
CALCULATE (
COUNT ( Table1[column1] ),
ALLEXCEPT ( Table1, Table1[column1] ),
Table1[Column2] <= EARLIER ( Table1[Column2] )
)
See also:
Cumulative Total DAX Pattern
Cumulative sum on different columns grouped by date and filtered differently
any help on this is greatly appreciated.
I'm looking to count the rows in a table where the PersonID is in a subquery list with a filter condition. In SQL it would look like this.
select count(*)
from tableA
where PersonId in(select distinct PersonId from tableA where CallResult = 1)
tableA has the same PersonId multiple times for each day and I'm looking to count how many times that PersonId is in the table but only if the PersonId has CallResult = 1 in any row within the table. There are other PersonId that don't have CallResult = 1 and I'm not looking to count those.
Maybe I'm overthinking this one but dax isn't my strength
PersonId | CallResult | CallNumber |
AB12 1 3
AB12 0 2
AB12 0 1
CD21 0 2
CD21 0 1
EF32 1 2
EF32 0 1
In this example I would expect the subquery to return AB12 and EF32 and the count to be 5 (3+2) calls
I may have figured it out. Posting here in case it helps anyone.
Count = CALCULATE(
SUM(TableA[Calls]),
CALCULATETABLE(
SUMMARIZE(TableA,TablA[PersonID]),
TableA[CallResult]=1
)
)
I have two tables A and B as shown below. The AccountID in A has a relationship with the AccountID in B.
A
AccountID CmpName AccFlag SysStartTime
A1 Test1 1 1/1/2020
A2 Test2 0 1/2/2020
A3 Test3 1 1/2/2020
B
ContactId AccountID ConFlag SysStartTime
C1 A1 1 1/1/2020
C2 A1 1 1/1/2020
C3 A1 0 1/1/2020
C4 A2 1 1/2/2020
I want to get the count of records in A that have 3 related records in B. I did this using a calculated column with the DAX:
getcount = COUNTROWS(RELATEDTABLE(B))
And then created another calculated column to flag the one's with getcount = 3.
But the problem is that I want to check the count of records in A that have 3 related records in B at a given time. So I need to filter by the sysStartTime's in both the tables. For example, I want to get the count of records in A that have 3 related records in B as of 1/1/2010. So the result should be 1. Please advise on how I can do this using a Measure instead of a calculated column.
You should be able to do something like this:
SUMX ( A, IF ( COUNTROWS ( RELATEDTABLE ( B ) ) = 3, 1 ) )
Assuming you have a date table with a relationship to both A and B, a slicer on that date table will apply to the measure and you can set whatever dates you want.