how to remove extra spaces in Power Query - powerquery

I want to remove extra spaces from the text, i referenced the code from the internet as below:
(text as text)=>
let
x = Text.Split(text," "),
y = Text.Select(x,each _<>""),
z = Text.Combine(y," ")
in
z
when i apply this function for my data , it show the error is "Expression.Error: We cannot convert a value of type List to type Text." , my column is definitely is text format already , i don't know root of the issue, could you please help look ?
my data is very simple, like below:

You can use below code as a custom function:
(text as text, optional char_to_trim as text) =>
let
char = if char_to_trim = null then " " else char_to_trim,
split = Text.Split(text, char),
removeblanks = List.Select(split, each _ <> ""),
result=Text.Combine(removeblanks, char)
in
result

Related

I wrote a code to update the Lettering of the first name in Zoho but it's not working

Here's the deluge script to capitalize the first letter of the sentence and make the other letters small that isn't working:
a = zoho.crm.getRecordById("Contacts",input.ID);
d = a.get("First_Name");
firstChar = d.subString(0,1);
otherChars = d.removeFirstOccurence(firstChar);
Name = firstChar.toUppercase() + otherChars.toLowerCase();
mp = map();
mp.put("First_Name",d);
b = zoho.crm.updateRecord("Contacts", Name,{"First_Name":"Name"});
info Name;
info b;
I tried capitalizing the first letter of the alphabet and make the other letters small. But it isn't working as expected.
Try using concat
Name = firstChar.toUppercase().concat( otherChars.toLowerCase() );
Try removing the double-quotes from the Name value in the the following statement. The reason is that Name is a variable holding the case-adjusted name, but "Name" is the string "Name".
From:
b = zoho.crm.updateRecord("Contacts", Name,{"First_Name":"Name"});
To
b = zoho.crm.updateRecord("Contacts", Name,{"First_Name":Name});

PowerQuery: Replace only values that are a date

I'm trying to replace any cell that has a date in it with "null" so that I can fill values in PowerQuery. I've used formula's that can can perform the replacement, but the non-date values that I want to remain as text are replaced with "Error". Is there a way to do this?
Something like this might work
#"RemoveDates" = Table.TransformColumns(#"PriorStepNameGoesHere",{{"Date", each try if Number.From(Date.From(_))>0 then null else _ otherwise _, type text}})
x = Table.TransformColumns(Source,{{"Column1", (a)=> try Date.FromText(a) otherwise null , type text}})

String Splitting in Foxpro Visual 9

I have a column of strings separated by comma.
Example: City, Zipcode
I want to make a column with only city populated so everything before the comma.
How has anyone else accomplished this? I know with Foxpro you can usually accomplish the same task various ways. Any help would be appreciated.
EDIT: SOLUTION
GETWORDNUM(FIELD,1,",")
This worked to give the text string before the comma from the column FIELD.
The easiest way to do that is to use STREXTRACT(). ie:
lcColumnData = "City, Zipcode"
? STREXTRACT(m.lcColumnData, "",",")
STORE ALINES(aCZ, "Atlanta, 30301", ",") TO iCZ
City = aCZ[1]
ZipCode = aCZ[2]
?City
?ZipCode
Try this:
str = "City, Zipcode"
*initialize the column value leftcol
leftcol = ''
*find comma position
pos = At(',', str)
Do Case
Case pos > 1
* there is a comma and something before that. take everything before that pos
leftcol = Left(str, pos-1)
Case pos = 1
* first char is comma
leftcol = ''
Otherwise
*there is no comma. take the whole string
leftcol = str
EndCase

How to fix the Power query code error for splitting a text column based on a criteria

Split the text values in a column based on the data type of the first character in each record.
I need to have the new (custom) column return the text before the first " " delimiter if the first character of the text is a number, otherwise return "0,".
If Value.Is(Text.Start([ConsumerAddress],1), type number) Then
Text.BeforeDelimiter([ConsumerAddress]," ") else "0,"
I need to have the new (custom) column return the text before the first " " delimiter if the first character of the text is a number, otherwise return "0,".
I don't think Value.Is is quite what you want. I would recommend a try otherwise construction along with Number.FromText like this:
= Table.AddColumn(#"Previous Step", "Custom",
each try Number.FromText(Text.BeforeDelimiter([ConsumerAddress], " "))
otherwise 0
)
If the text before the first space can be converted to a number, then that's what you get. If it can't the Number.FromText throws an error and you get the 0 from the otherwise specification.
Edit: If you want the criterion for the first character only, try this:
= Table.AddColumn(#"Previous Step", "Custom",
each if (try Number.FromText(Text.Start([ConsumerAddress], 1)) otherwise 0) <> 0
then Text.BeforeDelimiter([ConsumerAddress], " ")
else "0"
)
This will return "12b" from "12b Maple St" whereas the first version would return 0 since "12b" can't be converted into a number.

How to Find Record Set Value is Number or String?

Original:
Using VB6
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Getting Type MisMatch Error.
How To Find Record Set Value is String or Number
Exactly i need
If Number means print number like Time Format (HH:MM:SS)
else
print string value
Coding Help for the above condition
Edited Version:
I'm working with an ADO.Recordset object and am trying to determine the data type of a column at run-time. I need to handle the column value differently in my code depending on its underlying data type. If the column value is a string, I want to work with the value as-is. If it is a number, I want to treat the number as an packed time and convert it to HH:MM:SS format (i.e. the number 120537 would be converted to the string "12:05:37").
Below is some example code that demonstrates what I want to achieve. However, when I run this code I get a "Type Mismatch" error:
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Have a look at the Visual Basic 6 function library. There are functions that can help you determine the underlying type of a value.
There are quite a few but you might find these useful:
IsNumeric
IsDate
Based on this article, if rsCardEvent is an ADO recordset, you could check the Type property. Something like this:
Select Case rsCardEvent(4).Type
Case adBSTR, adChar, adVarChar, adWChar, _
adVarWChar, adLongVarChar, adLongVarWChar
' It is a string '
Case Else
' It is not a string '
End Select
You can use the IsNumeric function available in VB6.
How about:
If TypeName(rsCardEvent(4).Value) = "String" then
http://msdn.microsoft.com/en-us/library/5422sfdf.aspx

Resources