The “[verb]/I had been” part is perfectly idiomatic, and is really the only way of talking about something that occurred continuously over a period preceding some point which is itself in the past.
Neither whole sentence is fully idiomatic, in my opinion, because of “at that time” in the first sentence and “the past” in the second. The impact of “at that time” is already contained in “when”, and the impact of “(for) the past (nine months)” is already part of “had been”. Same goes for “the last” in the first sentence.
Therefore, these two sentences would probably instead be written:
When I joined [X company], I had been unemployed for three years.
When I started learning to swim, I had been practicing soccer for nine months.