cancel
Showing results for
Did you mean:
Highlighted

## Problems converting control.OnSelect formula to control.Text

Hi,

I am having a formula which currently runs on Button.OnSelect without problems. The formula converts a hex value of any length to its decimal equivalence.

This formula has to be converted because it should run now in a Label.Text property.

The problem I'm having is to replace the Collect and CountRows function. I tried a lot of different things but I always struggle with the CountRows() formula that is needed to set the record number of a table (which is needed for calculation).

It would be very nice if you could throw an eye on it.

Thomas

Steps to run the current code:

• Create a Label (hexString.Text = "#4A1A")
• Create a Label (Label.Text = _res)
• Create a Button (Button.OnSelect = <copy the code from below>)

``````// convert a hex value string of any length to its decimal equivalence
// testing with the following hexString.Text properties:
//   '#4A1A', '#4a1a', '4A1A' or '4a1a'
// expected result:
//   18970
Clear(coll1);
ForAll(
// split the hex string into a single column table ["4", "A", "1", "A"]
// or ["4", "a", "1", "a"]
// the table will contain 4 records
Split(
If(Left(hexString.Text, 1) <> "#", hexString.Text,
Mid(hexString.Text, 2, Len(hexString.Text) -1)
),
""
),

// fill collection 'coll1' based on split table ["4", "A", "1", "A"]
// calculation per record:
//   Find(Upper(Result)...) -1 returns a value between 0 and 15
//   (the decimal equivalent for the given hex char)
//     "4" => 4
//     "A" => 10
//     "1" => 1
//     "A" => 10
//   multiply this by
//   Power(16, (length of the hex string (minus 1 if starts with '#')) - 1
//   - (current number of records in collection (starts with 0)) -1
//     Power(16, 3 - 0) => Power(16, 3) => 4096
//     Power(16, 3 - 1) => Power(16, 2) =>  256
//     Power(16, 3 - 2) => Power(16, 1) =>   16
//     Power(16, 3 - 3) => Power(16, 0) =>    1
//   records:
//     {decValue:  4 * 4096} => 16384
//     {decValue: 10 *  256} =>  2560
//     {decValue:  1 *   16} =>    16
//     {decValue: 10 *    1} =>    10
Collect(coll1,
{
decValue: (
Find(Upper(Result), "0123456789ABCDEF") -1)
* Power(16,
Len(hexString.Text) -1 - If(Left(hexString.Text, 1) = "#", 1, 0)
- CountRows(coll1)
)
}
)
);
// _res = 18970 (16384 + 2560 + 16 + 10)
// _res is set as Label.Text property to display the result
Set(_res, Sum(coll1, decValue))``````

7 REPLIES 7
Highlighted
Resolver I

Hi TxH,

If I understand correctly as per your example, if you provide hex value of "#4A1A" it should return the result 18970, which it is not displaying on the label.

I tried your setup and by making some changes i was able to get the expected result.

1. Add a global variable. Set(res,0)

2. Set Label.Text = res

3. Change last lines of code to below-

UpdateContext({res: Sum(coll1, decValue)})

Hope this helps.

Highlighted

Hi DanishIslam,

Ups, the Label.Text property is _res (with a leading underscore), not res.

Do you have an idea how I have to change the formula to run in a Label.Text property?

Highlighted
Resolver I

Hi Thomas,

Cant you change your hexString control to Text Input instead of Label ?

In that case, you can use your formula on "On Change" property of control and rest of the changes i mentioned remains same.

So whenever hex value is entered in text box and focus is moved, label will display the calculated decimal value.

Highlighted

No, I can't do that because the hex strings are not mutable and are part of a DataSource.
I'm just using a Label in the post to make it easier to replicate for you.

Highlighted
Resolver I

Then i need to understand when you are getting this data and when it needs to be converted to display on label. Because label cannot run this formula and display it.

Formula needs to be called from some other event like on screen visible, on data collected from data source etc.

Highlighted

I thought the solution could be something like the code shown below but unfortunately it does not workit returns 21520 instead of 18970.

The problem lies in the formula Find(Result, hexStr)). It always returns the index of the first element found. So for the second 'a' in "4A1A" it returns 2 which is the wrong one - it should be 4.

I need some peace of code which replaces the first found char with an 'X' so the next record with the same hex character (the second 'a') will find the correct one.

This is how it should work:

0. "4A1A"
1. "4" => returns 1 => set search field  to "XA1A"

2. "A" => returns 2 => set search field  to "XX1A"

3. "1" => returns 3 => set search field  to "XXXA"

4. "A" => returns 4 => set search field  to "XXXX"

Code in Label1.Text:

``````Sum(
ForAll(
Split(
If(Left(hexString.Text, 1) <> "#", hexString.Text,
Mid(hexString.Text, 2, Len(hexString.Text) -1)
),
""
),
"hexStr", hexString.Text,
"decChar", Find(Upper(Result), "0123456789ABCDEF") -1
),
"decValue", decChar * Power(16, Len(hexString.Text) - Find(Result, hexStr))
),
Patch(
{dummy: Blank()},
{decValue: decValue, hexStr: Substitute(hexStr, Result, "X", 1)}
)
),
decValue
)``````

Highlighted

* Sorry, must be piece instead of peace🙄

Announcements

#### Power Platform Community Conference

Check out the on demand sessions that are available now!

#### Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

#### Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

#### Community Highlights

Check out the Power Platform Community Highlights

Top Solution Authors
Top Kudoed Authors
Users online (7,307)