INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Help with field defined by calculated data  (Read 2897 times)

8139david

  • Galactic Citizen
  • ****
  • Posts: 345
Help with field defined by calculated data
« on: February 20, 2016, 02:19:21 pm »

For my classical music, I use Artist for composers, and Album Artist for performers (soloists, conductor, orchestra).
I have created the custom field Artist (Auto), which I want to behave this way:
1) When Artist is empty, Artist (Auto) = - Album Artist, eg it would show '- Gould'.
2) When Artist is not empty, Artist (Auto) = Artist - Album Artist, eg it would show: 'Bach - Gould'.
I've looked at the wiki and tried several expressions. But it doesn't work as I want.
Here is one of my attempts:
if(!isempty([artist]), [artist] - [album artist], - [album artist])
How should I define the field?
Logged

8139david

  • Galactic Citizen
  • ****
  • Posts: 345
Re: Help with field defined by calculated data
« Reply #1 on: February 20, 2016, 02:30:17 pm »

I found something that works, but I don't know why the previous one wasn't good:
If(IsEmpty([Artist], 0), - [Album Artist], [Artist] - [Album Artist])
Logged

ferday

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1732
Re: Help with field defined by calculated data
« Reply #2 on: February 20, 2016, 06:34:40 pm »

the first one is probably because you should use ifelse() instead of if()

ifelse(!isempty([artist],0),-[artist],[artist]-[album artist])

edit:  tested my own guess and I'm not sure I'm right.  I think it should've worked the first go
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Help with field defined by calculated data
« Reply #3 on: February 21, 2016, 09:20:53 am »

This expression looks like it is working correctly for me:

Code: [Select]
if(!isempty([artist]), [artist] - [album artist], - [album artist])
That says:

  • If [Artist] is filled (not empty) then output: [Artist] - [Album Artist].
  • If [Artist] is empty, output: - [Album Artist]


That seems to be working perfectly well on my files. However, that's not the way I'd do it. You're making it much more complex than is needed. Your desired output is almost equivalent to this:
Code: [Select]
[Artist] - [Album Artist]
That will do basically the same thing. When any field value is empty, it is omitted in the output of the expression, so [Artist] will be empty when it is empty. The problem with that is only the space before the dash, which will always output (whether [Artist] is empty or not).  So the problem is that when [Artist] is empty, you'll get a leading blank space before the dash.  So, a simple way to fix that would be to use FixSpacing():
Code: [Select]
FixSpacing([Artist] - [Album Artist])
Or, if you only want to show the space-dash-space when [Artist] is filled, then use Delimit():
Code: [Select]
Delimit([Artist], / -/ )[Album Artist]
And, finally, if you never want to see the space-dash-space unless both [Artist] and [Album Artist] are filled, then:
Code: [Select]
If(IsEmpty([Album Artist]),[Artist],Delimit([Artist], / -/ )[Album Artist])
But really, if that's what you want, you don't need any fancy Delimit() expressions. You could just use Clean() to remove the extraneous spaces and dashes:
Code: [Select]
Clean([Artist] - [Album Artist])
I'm not sure what you're going for, but all of these work right:


EDIT: In my screenshot above I forgot to escape the space after the dash in the Delimit() examples originally, so they have no space before [Album Artist]. I fixed the example code above, but didn't re-do the screenshot because I'm lazy.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

8139david

  • Galactic Citizen
  • ****
  • Posts: 345
Re: Help with field defined by calculated data
« Reply #4 on: March 03, 2016, 06:12:44 am »

In the end, I used this definition for the custom field:
If(IsEmpty([Artist], 0), - [Album Artist], If(IsEmpty([Album Artist], 0), [Artist], [Artist] - [Album Artist]))
Logged

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8014
Re: Help with field defined by calculated data
« Reply #5 on: March 03, 2016, 07:38:53 am »

In the end, I used this definition for the custom field:
If(IsEmpty([Artist], 0), - [Album Artist], If(IsEmpty([Album Artist], 0), [Artist], [Artist] - [Album Artist]))

That logic looks backwards to me.  You're testing for [Artist], then using [Album Artist] without testing it first.

Brian.
Logged

8139david

  • Galactic Citizen
  • ****
  • Posts: 345
Re: Help with field defined by calculated data
« Reply #6 on: March 03, 2016, 07:51:12 am »

You're right.

In fact, I can simplify the formula, since [Album Artist] is never empty.
So it should be as it was on my second post):
If(IsEmpty([Artist], 0), - [Album Artist], [Artist] - [Album Artist])
Logged
Pages: [1]   Go Up