web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Problems converting co...
Power Apps
Unanswered

Problems converting control.OnSelect formula to control.Text

(0) ShareShare
ReportReport
Posted on by 174

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.

 

Thanks in advance

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))

 

 

Categories:
  • DanishIslam Profile Picture
    108 on at

    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.

  • TxH Profile Picture
    174 on at

    Hi DanishIslam,

    Thanks for your quick response.

    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?

     

  • DanishIslam Profile Picture
    108 on at

    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.

  • TxH Profile Picture
    174 on at

    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. 

  • DanishIslam Profile Picture
    108 on at

    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.

  • TxH Profile Picture
    174 on at

    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(
     AddColumns(
     AddColumns(
     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
     )

     

     

     

  • TxH Profile Picture
    174 on at

    * Sorry, must be piece instead of peace🙄

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 397 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 354

#3
WarrenBelz Profile Picture

WarrenBelz 232 Most Valuable Professional

Last 30 days Overall leaderboard