How to conditionally transform text in a column in power query? - powerbi

I am building a workbook in PowerBI and I have the need for doing a conditional appending of text to column A if it meets a certain criteria. Specifically, if column A does not end with ".html" then I want to append the text ".html" to the column.
A sample of the data would look like this:
URL | Visits
site.com/page1.html | 5
site.com/page2.html | 12
site.com/page3 | 15
site.com/page4.html | 8
where the desired output would look like this:
URL | Visits
site.com/page1.html | 5
site.com/page2.html | 12
site.com/page3.html | 15
site.com/page4.html | 8
I have tried using the code:
#"CurrentLine" = Table.TransformColumns(#"PreviousLine", {{"URL", each if Text.EndsWith([URL],".html") = false then _ & ".html" else "URL", type text}})
But that returns an error "cannot apply field access to the type Text".
I can achieve the desired output in a very roundabout way if I use an AddColumn to store the criteria value, and then another AddColumn to store the new appended value, but this seems like an extremely overkill way to approach doing a single transformation to a column. (I am specifically looking to avoid this as I have about 10 or so transformations and don't want to have so many columns to add and cleanup if there is a more succinct way of coding)

You don't want [URL] inside Text.EndWith. Try this:
= Table.TransformColumns(#"PreviousLine",
{{"URL", each if Text.EndsWith(_, ".html") then _ else _ & ".html", type text}}
)

Related

Django Exclude Model Objects from query using a list

Having trouble with my query excluding results from a different query.
I have a table - Segment that I have already gotten entries from. It is related
to another table - Program, and I want to also run the same query on it but I want to exclude
any of the programs that were already found during the segment query.
When I try to do it, the list isn't allowed to be used in the comparison... See below:
query = "My Query String"
segment_results = Segment.objects.filter(
Q(title__icontains=query)|
Q(author__icontains=query)|
Q(voice__icontains=query)|
Q(library__icontains=query)|
Q(summary__icontains=query) ).distinct()
# There can be multiple segments in the same program
unique_programs = []
for segs in segment_results:
if segs.program.pk not in unique_programs:
unique_programs.append(segs.program.pk)
program_results = ( (Program.objects.filter(
Q(title__icontains=query) |
Q(library__icontains=query) |
Q(mc__icontains=query) |
Q(producer__icontains=query) |
Q(editor__icontains=query) |
Q(remarks__icontains=query) ).distinct()) &
(Program.objects.exclude(id__in=[unique_programs])))
I can run:
for x in unique_programs:
p = Program.objects.filter(id=x)
print("p = %s" % p)
And I get a list of Programs...which works
Just not sure how to incorporate this type of logic into the results
query...and have it exclude at the same time. I tried exclude keyword,
but the main problem is it doesn't like the list being in the query - I get an
error:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'.
Feel like I am close...
The answer is simple, I was not comparing objects correctly in the filters, so
the correct statement would be:
program_results = (Program.objects.filter(
Q(title__icontains=query) |
Q(library__icontains=query) |
Q(mc__icontains=query) |
Q(producer__icontains=query) |
Q(editor__icontains=query) |
Q(remarks__icontains=query) )&
(Program.objects.exclude(id__in=Program.objects.filter(id__in=unique_programs))))

DAX to Test for Whole Number

I have a Actuals column like so:
ID | Airport
------------
A | 98.4
B | 98.0
C | 95.3
I'm attempting to format the numbers above into percentages for a front-end report. I have this written in a switch statement - for ease I'll just write the logic as an IF boolean.
example_measure =
VAR Nums = SELECTEDVALUES(Table[Actuals])
VAR FormatNums = IF(DIVIDE(ROUND(nums,1), nums) = 1,
format(nums,"0%"),format(nums,"0.0%")
-
RETURN
FormatNums
no matter what I do this always returns a number with a floating point value of 1f
so in my raw data of 98.0 I'm expecting the format to return "98%" rather than "98.0%"
the measures are used on individual cards, and are filtered so they can only ever show one value or blank, meaning they will only ever display one value on their own.
I've toyed with using if(nums = int(nums) which should evaluate to true for 98.0 but it I always get false.
There is a simpler way - just use built-in formatting codes:
Formatted Actuals =
VAR x = SELECTEDVALUE(Data[Actuals])
RETURN
FORMAT(x, "General Number") & "%"
Result:
Built-in style "General Number" handles your situation correctly, you just need to add % symbol to the formatted string. No need to test for whole numbers.
To convert a column/measure into percentage, you can simply divide the column by 100 and then format it as a percentage. Use the following steps:
Create a new column/measure: Perc_value = Table[Actuals]/100
Then go into the modelling tab, select the created column/measure and format it as a % and limit the number of decimal places to 0
This should give the view you are looking for. Hope this helps.
Edit:
You can use the below formula to achieve the desired result:
Column4 = IF('Table'[Column2]-ROUND('Table'[Column2],0)=0,FORMAT('Table'[Column2]/100,"0%"),FORMAT('Table'[Column2]/100,"0.0%"))
Replace Column2 withyour field and you should be good to go.

How to optimize regexp_replace performance for multiple substrings in PostgreSQL

I need to remove multiple substrings from a column of strings. I have ~200k rows, as well as a large bag of custom "stopwords."
Specifically, I have a table (NameTable), with a Names column full of multiple-word strings. Only a small subset of the words in each string is relevant for what I'm doing next. names has a trigram/GIN index. Is there any way to speed up a query like the following, but with a much larger string of target words? I'm using PG10.
...
Create Index Names_trgm on NameTable using gin(Name gin_trgm_ops);
Update NameTable
set CleanedName = regexp_replace(Name, '( fuzzy| wuzzy| was| a | bear | the |
first| time | yossarian| saw | the | chaplain | he | fell| madly| in | love|
with| him)',' ','g');

Select a row and column from a query in coldfusion 10

Can I select a certain row/column combination in coldfusion without doing a query of queries? For example:
Some Query:
ValueToFind | ValueToReturn
String 1 | false
String 2 | false
String 3 | true
Can I somehow do #SomeQuery["ValueToFind=String 3"][ValueToReturn]# = true without doing a query of queries ? I know there's code out there to get a certain row by id, but I'm not sure how or if I can do it when I need a string as the ID
If this can't be done, is there a short hand way to set up a coldfusion function so I can use something like FindValue(Query, "String 3") and not have to use ?
You can treat a query column as an array.
yourRow = ArrayFind(queryName['columnName'], "'the value you seek'");
If you get a zero, the value you seekis not there.
Edit starts here:
For values of other columns in that row, simply use that variable.
yourOtherValue = queryName.otherColumnName[yourRow];
A small modification to Dan's code, you can find the column value using the code below
yourVaue = SomeQuery["ValueToReturn"][ArrayFind(SomeQuery['ValueToFind'], "String 3")]

BIRT: Align rows in list element

I'm using the Birt list element to display my data from left to right. (see this question as reference). Eg. List element with a Grid in details and the grid set to inline.
The issue I'm facing now is, that the different rows in the grid are not aligned left to right (probably due to some rows having empty values in some fields). How can I force BIRT to align properly?
EDIT:
This is especially also a problem with longer text that wraps to more than 1 line. The wrapping /multiple lines should be reflected by all list elements in that "row of the output".
Unfortunately, I don't see any chance to accomplish this easily in the generic case - that is, if the number of records is unknown in advance, so you'd need more than one line:
student1 student2 student3
student4 student5
Let's call those line "main lines". One main line can contain up to 3 records. The number 3 may be different in your case, but we can assume it is a constant, since (at least for PDF reports) the paper width is restricted.
A possible solution could work like this:
In your data set, add two columns for each row: MAIN_LINE_NUM and COLUMN_NUM, where the meaning is obvious. For example, this could be done with pure SQL using analytic functions (untested):
select ...,
trunc((row_number() over (order by ...whatever...) - 1) / 3) + 1 as MAIN_LINE_NUM,
mod(row_number() over (order by ...whatever...) - 1), 3) +1 as COLUMN_NUM
from ...
order by ...whatever... -- The order must be the same as above.
Now you know where each record should go.
The next task is to transform the result set into a form where each record looks like this (for the example, think that you have 3 properties STUDENT_ID, NAME, ADDRESS for each student):
MAIN_LINE
STUDENT_ID_1
NAME_1
ADDRESS_1
STUDENT_ID_2
NAME_2
ADDRESS_2
STUDENT_ID_3
NAME_3
ADDRESS_3
You get the picture...
The SQL trick to achieve this is one that one should know.
I'll show this for the STUDENT_ID_1, STUDENT_ID_2 and NAME_1 column as an example:
with my_data as
( ... the query shown above including MAIN_LINE_NUM and COLUMN_NUM ...
)
select MAIN_LINE_NUM,
max(case when COLUMN_NUM=1 then STUDENT_ID else null end) as STUDENT_ID_1,
max(case when COLUMN_NUM=2 then STUDENT_ID else null end) as STUDENT_ID_2,
...
max(case when COLUMN_NUM=1 then NAME else null end) as NAME_1,
...
from my_data
group by MAIN_LINE_NUM
order by MAIN_LINE_NUM
As you see, this is quite clumsy if you need a lot of different columns.
On the other hand, this makes the output a lot easier.
Create a table item for your dat set, with 3 columns (for 1, 2, 3). It's best to not drag the dataset into the layout. Instead, use the "Insert element" context menu.
You need a detail row for each of the columns (STUDENT_ID, NAME, ADDRESS). So, add two more details rows (the default is one detail row).
Add header labels manually, if you like, or remove the header row if you don't need it (which is what I assume).
Remove the footer row, as you probably don't need it.
Drag the columns to the corresponding position in your table.
The table item should look like this now (data items):
+--------------+--------------+-------------+
+ STUDENT_ID_1 | STUDENT_ID_2 | STUDENT_ID3 |
+--------------+--------------+-------------+
+ NAME_1 | NAME_2 | NAME_3 |
+--------------+--------------+-------------+
+ ADDRESS_1 | ADDRESS_2 | ADDRESS_3 |
+--------------+--------------+-------------+
That's it!
This is one of the few examples where BIRT sucks IMHO in comparison to other tools like e.g. Oracle Reports - excuse my Klatchian.